简体   繁体   English

Powershell排序对象

[英]Powershell Sort-Object

I have an Array of "xml-node" objects: 我有一个“xml-node”对象数组:

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; 我想根据“修改”属性对这个xml节点数组进行排序; 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. 您还可以使用ScriptBlock对对象进行排序。 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. 显然,它本身并不是很有用,但是如果你想以某种方式对对象进行排序而不仅仅是属性,你可以操作ScriptBlock中的属性。

For example to sort processes by the last chatacter in the process name. 例如,按进程名称中的最后一个chatacter对进程进行排序。

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: 这是一个证明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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM