1

I'm pretty new to Powershell and have a problem. It seems I can get neither the Compare-Object nor the Sort-Object funtions to work. I got two Arraylists which I fill with 10 objects of type "Table".

#Create Arraylists
$list1 = new-object System.Collections.ArrayList
$list2 = new-object System.Collections.ArrayList


#Declare Table-Class
class Table {
    [String] $name
    [Int] $number
}


#Fill list1
[Int] $i = 0
while ($i -lt 10) {
    $entry = new-Object Table
    $entry.name = "Name";
    $entry.number = $i;
    $list1.Add($entry)
    $i++
}

#Fill list2
[Int] $j = 10
while ($j -gt 0) {
    $entry = new-Object Table
    $entry.name = "name";
    $entry.number = $j;
    $list2.Add($entry)
    $j--
}

Now I want to compare these two ArrayLists like this:

Compare-Object $list1 $list2 | ForEach-Object {$_.InputObject}

This doesn't seem to work and I think it's because I'm not really doing anything with the comparison. If someone could help me with the correct Syntax I'd be really happy. Anyway, I think this comparison would somehow return a $false boolean. If that were true, I'd like to sort $list2 by $entry.number.

I'm attempting this like that:

$list2 = $list2 | Sort-Object -Property { $_[1] }

But my list doesn't change at all. I already tried several different "solutions" but it seems none of these is a real "solution".

I'd be really glad if someone could help me or at least point me in the right direction.


EDIT: For future readers, here is the working syntax:

#sort list by the property number
$list2 = $list2 | sort-object -Property number

#count differences and if differences greater than 0, do stuff
[Int] $diff
$properties = $list1 | Get-Member -MemberType Property | Select-Object -ExpandProperty Name

foreach ($property in $properties) {
    $diff = 0
    $diff = (Compare-Object $list1 $list2 -Property "$property").count
    if ($diff -ne 0) {
        #do stuff
    }
    else {
        #do something else
    }
}
Clijsters
  • 4,031
  • 1
  • 27
  • 37
Obongo
  • 37
  • 1
  • 9
  • Start by reading ***Get-Help Compare-Object*** and ***Get-Help Sort-Object***. I'm not sure that they do exactly what you think they do. – EBGreen Dec 01 '17 at 14:33
  • If one of the answers below fixes your issue, you should accept it (click the check mark next to the appropriate answer). That does two things. It lets everyone know your issue has been resolved to your satisfaction, and it gives the person that helps you credit for the assist. [See here](http://meta.stackexchange.com/a/5235) for a full explanation. – Clijsters Dec 05 '17 at 09:57

1 Answers1

2

Your sorting does not work because -Property expects you to pass the name of an actual property. In your case the class two properties: $name and $number. You can also check what properties an object has by using $list2 | Get-Member. So in order to sort, you can use:

$list2 = $list2 | Sort-Object -Property number

Now the reason Compare-Object is not working is because it's implemented differently that one might expect. See this answer explaining how it works.

One other thing you should keep in mind: Calling $list2.Add($entry) actually returns an integer (the index of the inserted element). PowerShell returns all uncaptured output. This can cause some unexpected behavior/output if you are not careful. So you should get in the habit of writing [void] $list2.Add($entry), unless you really want to return those indexes.

Thomas Glaser
  • 1,670
  • 1
  • 18
  • 26
  • And another question, is it possible to get a boolean from the comparison? So I can save that in a variable and then execute some code depending if they are the same or different. Thanks so much, very helpful! – Obongo Dec 01 '17 at 15:06
  • 1
    @Obongo I think `Compare-Object` returns nothing if it's equal. So a simple `$isEqual = ( Compare-Object .. ) -eq $null` should do the trick. – Thomas Glaser Dec 05 '17 at 11:14