-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Add -Top parameter to Sort-Object #2462
Description
As per the discussion on pull request #2446 that I recently closed, it would be very useful to have a -Top parameter on Sort-Object. This parameter would allow users to indicate they only want the top N results from their sort. The original pull request I referenced tried to address the need for -Top by adding -Top and -Bottom parameter aliases to Select-Object, but as @lzybkr pointed out, it makes more sense to have a new -Top parameter (not alias) on Sort-Object instead since Top implies sorting.
This issue should be marked as Issue-Enhancement and I would like it to be assigned to me as I have already started work on it. See the discussion on #2446 for details.
The current syntax of Sort-Object is as follows:
Sort-Object [[-Property] <Object[]>] [-Descending] [-Unique] [-InputObject <psobject>]
[-Culture <string>] [-CaseSensitive] [<CommonParameters>]
The new syntax I would like to propose is as follows:
Sort-Object [[-Property] <Object[]>] [-Descending] [-Unique] [-Top <int>] [-InputObject <psobject>]
[-Culture <string>] [-CaseSensitive] [<CommonParameters>]
This design approach avoids making assumptions about what -Top means in a given data set, requiring users to control what is top by using the -Descending parameter (e.g. in some data sets, the top items may be the lowest values in the set, such as priority, while in others, the top items may be the highest values in the set, such as workingset64). I believe this is really the only syntax that makes sense for this cmdlet.