Powershell排序对象

时间:2014-03-03 18:29:00

标签: arrays sorting powershell

我有一个“xml-node”对象数组:

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

Node:
filename
modification
type

问题:

我想根据“修改”属性对这个xml节点数组进行排序; 我该怎么做呢?

我试过了:

$nodes | sort-object Node.modification 

2 个答案:

答案 0 :(得分:21)

仅使用属性名称按对象的直接属性进行排序。

$nodes | sort-object modification

您还可以使用ScriptBlock对对象进行排序。所以这也可行:

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

显然,它本身并不是很有用,但如果你想以某种方式对对象进行排序而不仅仅是属性,你可以操作ScriptBlock中的属性。

例如,按流程名称中的最后一个聊天计划对流程进行排序。

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

编辑:

要访问酒店的财产:

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

答案 1 :(得分:1)

这是一个证明Rynant解决方案确实有效的样本:

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