Properties for sorting must be specified as strings. Since the indices to arrays are integers, not strings, it is quite odd to have to specify them as strings.
(,2), (,12) | SORT 0 -T:1 | SHOULD -BE 2
Hi @yecril71pl If a string is specified for -Property, that string is used as the _name_ of the property to sort on. However, as well as a string, you can specify a ScriptBlock which will be used to compute the _value_ to sort on for each input item. For example,
powershell
@((1,2),(3,1)) | sort {$_[1]} # sort on the second element in a list of pairs.
This is the most general solution and will let you do whatever you want.
Consider, however, the absurdity of the following call, which does work:
(,2), (,12) | SORT '0' -T:1 | SHOULD -BE 2
Also, I suppose that declarative calls should be preferred to procedural calls, as they are easier to understand and probably more efficient.
@yecril71pl In fact, it doesn't actually work: replacing "0" with "zork" produces the same result. If you add more elements to the array, you'll see that it isn't sorting at all. What's happening is that sort tries to access the property 0 on each object. Since there is no such property, the property accessor code returns null. It does this for each object so all objects are considered equal with the result that sort effectively does nothing.
Also, I suppose that declarative calls should be preferred to procedural calls, as they are easier to understand and probably more efficient.
It's actually not declarative. PowerShell is polymorphic but not generic so everything is resolved at runtime.
Note 1: So why doesn't generate an error if the property is missing? Because PowerShell deals with heterogeneous streams. Consider gci | sort length. The gci command returns a mixture of FileInfo and DirectoryInfo object. DirectoryInfo objects don't have a Length property so generating an error on a missing property would cause the sort to fail. Ignoring missing properties makes the interactive experience generally much simpler.
Note 2: Why do we allow 0 as a property name when it's not a legal .NET name? Because the PowerShell type system adapts arbitrary objects that don't have the same restrictions as .NET. For example, you can use dot notation to access XML elements.
This issue has been marked as answered and has not had any activity for 1 day. It has been closed for housekeeping purposes.
Most helpful comment
@yecril71pl In fact, it doesn't actually work: replacing "0" with "zork" produces the same result. If you add more elements to the array, you'll see that it isn't sorting at all. What's happening is that
sorttries to access the property0on each object. Since there is no such property, the property accessor code returns null. It does this for each object so all objects are considered equal with the result thatsorteffectively does nothing.It's actually not declarative. PowerShell is polymorphic but not generic so everything is resolved at runtime.
Note 1: So why doesn't generate an error if the property is missing? Because PowerShell deals with heterogeneous streams. Consider
gci | sort length. Thegcicommand returns a mixture ofFileInfoandDirectoryInfoobject.DirectoryInfoobjects don't have aLengthproperty so generating an error on a missing property would cause the sort to fail. Ignoring missing properties makes the interactive experience generally much simpler.Note 2: Why do we allow
0as a property name when it's not a legal .NET name? Because the PowerShell type system adapts arbitrary objects that don't have the same restrictions as .NET. For example, you can use dot notation to access XML elements.