문제

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 
도움이 되었습니까?

해결책

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 }

다른 팁

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top