How is the default sort property specified for an object? I’ve googled around and found several copies of the same instance where this question was asked, but either the answer isn’t the answer, or I don’t know what I’m looking at (probability of the latter approaches 100%).
For example,
Get-ChildItem | Sort-Object
sorts on name, even though no sort property was specified.
And the help for Sort-Object says this:
If you do not specify properties, the cmdlet sorts based on default properties for the object type.
The one answer I found for this referred to the FileSystem.Format.ps1xml file, but I don’t see anything in there that relates to sorting and/or ordering. I see that Name is specified as the WideEntry; does sorting also use that by default?
In short, where is Sort getting its information for the default sort property for an object?
My apologies for what my wife calls male pattern blindness, but where? In the segment you included, I see only a DefaultDisplayProperty, not a DefaultSortProperty (or anything similar). Nor do I see anything in either of the full .ps1xml files.
Further, on System.IO.FileInfo, e.g., there is no DefaultDisplayProperty, it is instead DefaultDisplayPropertySet and consists of three properties (LastWriteTime,Length,Name), while default sort uses only name. So I don’t see how DefaultDisplayPropertySet could be used as the DefaultSortProperty.
That is the script block for the BaseName ScriptProperty. It’s defining how BaseName is defined for directories (that script block is more elaborate for System.IO.FileInfo). I don’t see how it has anything to do with the default sort property.
Something like this used to trick me up. I’m only sending one property, but sort can’t figure out to sort it (it returns one item), unless I do select -expand or sort -u filename. Maybe open source will fix it.
This was a fun rabbit hole to go down, but my thought was that this was something defined as a PSStandardMember. This data is hidden by default, so it takes some digging to get which is discussed by Mr. Snover here: PSStandardMembers – The Stealth Property - PowerShell Team
You can see if this is defined in the object like so:
The DefaultDisplayProperty[Set] is visible in the types.ps1xml file. We’ve already seen it (Curtis’ post above). (It is nice to know that there’s a PS way to see that information as well, so thanks for that.)
However, neither of those have anything to do with the default sort property (which, from your reply, would seem to be DefaultKeyProperty[Set]). That doesn’t exist in the types.ps1xml file (for either FileInfo or DirectoryInfo; the only occurrence is for HistoryInfo).
In your first block above, you don’t show what $obj is, so we don’t know what object it’s showing the DefaultKeyPropertSet for, but it doesn’t appear to be for Get-ChildItem, at least from my testing.