9

I have an Array of "xml-node" objects:

xml-node object:
    node <---------- this object is the one that has 3 other attributes (see below)
    path
    pattern

Node:
filename
modification
type

Problem:

I want to sort this array of xml-nodes based on the "modification" attribute; how would I go about it?

I've tried:

$nodes | sort-object Node.modification 

2 Answers 2

26

Use the property name only for sorting by the object's immediate properties.

$nodes | sort-object modification

You can also use a ScriptBlock to sort objects. So this would work as well:

$nodes | sort-object { $_.modification }

Obviously that is not very useful by itself, but if you want to sort the objects in some way other than simply the property, you can manipulate the properties inside the ScriptBlock.

For example to sort processes by the last chatacter in the process name.

get-process| sort-object { $_.name[-1] }

Edit:

To access a property's property:

$nodes | sort-object { $_.node.modification }
Sign up to request clarification or add additional context in comments.

4 Comments

sorry, I had to edit my question; the "modification" attribute is one level deeper; that's where I'm a bit stuck;
Very helpful, thanks! so is it possible to get this as a sorted list; like, can I go: $x = $nodes | sort-object { $_.node.modification } or something to that effect (right now, $x is not sorted; I tried piping the result to a ... | select $_; that doesn't really work either?
Yes, $x = $nodes | sort-object { $_.node.modification } should result in $x having the sorted list.
You're absolutely right! Thanks Rynan. Plus one for you. I'll mark yours as answered; and post a complete sample example below just in case anyone else is wondering... thanks again!
1

Here's a sample that proves Rynant's solution actually works:

cls

$node1 = New-Object PSObject
Add-Member -InputObject $node1 -MemberType NoteProperty -Name fileName -Value "textfile1.txt";  
Add-Member -InputObject $node1 -MemberType NoteProperty -Name fileType -Value "text";  
Add-Member -InputObject $node1 -MemberType NoteProperty -Name modification -Value "2014-02-24";  
$node2 = New-Object PSObject
Add-Member -InputObject $node2 -MemberType NoteProperty -Name fileName -Value "textfile2.txt";  
Add-Member -InputObject $node2 -MemberType NoteProperty -Name fileType -Value "text";  
Add-Member -InputObject $node2 -MemberType NoteProperty -Name modification -Value "2014-03-01";  
$node3 = New-Object PSObject
Add-Member -InputObject $node3 -MemberType NoteProperty -Name fileName -Value "textfile3.txt";  
Add-Member -InputObject $node3 -MemberType NoteProperty -Name fileType -Value "text";  
Add-Member -InputObject $node3 -MemberType NoteProperty -Name modification -Value "2014-02-21";  
$node4 = New-Object PSObject
Add-Member -InputObject $node4 -MemberType NoteProperty -Name fileName -Value "textfile4.txt";  
Add-Member -InputObject $node4 -MemberType NoteProperty -Name fileType -Value "text";  
Add-Member -InputObject $node4 -MemberType NoteProperty -Name modification -Value "2014-02-22";  

$result1 = New-Object PSObject
Add-Member -InputObject $result1 -MemberType NoteProperty -Name Node -Value $node1;  
Add-Member -InputObject $result1 -MemberType NoteProperty -Name Path -Value "aaa";  
Add-Member -InputObject $result1 -MemberType NoteProperty -Name Pattern -Value "aaa/aaa[@aaa='aaa']";  

$result2 = New-Object PSObject
Add-Member -InputObject $result2 -MemberType NoteProperty -Name Node -Value $node2;  
Add-Member -InputObject $result2 -MemberType NoteProperty -Name Path -Value "bbb";  
Add-Member -InputObject $result2 -MemberType NoteProperty -Name Pattern -Value "bbb/bbb[@bbb='bbb']";  

$result3 = New-Object PSObject
Add-Member -InputObject $result3 -MemberType NoteProperty -Name Node -Value $node3;  
Add-Member -InputObject $result3 -MemberType NoteProperty -Name Path -Value "ccc";  
Add-Member -InputObject $result3 -MemberType NoteProperty -Name Pattern -Value "ccc/ccc[@ccc='ccc']";  

$result4 = New-Object PSObject
Add-Member -InputObject $result4 -MemberType NoteProperty -Name Node -Value $node4;  
Add-Member -InputObject $result4 -MemberType NoteProperty -Name Path -Value "ddd";  
Add-Member -InputObject $result4 -MemberType NoteProperty -Name Pattern -Value "ddd/ddd[@ddd='ddd']";  


$results = @()
$results += $result1
$results += $result2, $result3, $result4

$x = $results | sort-object { $_.Node.modification }; 
$y = $results | sort-object { $_.Node.modification } -desc; 

$x
$y

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.