Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell Sort-Object

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 
like image 342
DotNet98 Avatar asked Mar 03 '14 18:03

DotNet98


1 Answers

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 }
like image 194
Rynant Avatar answered Oct 13 '22 08:10

Rynant