New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Command Explorer doesn't sort like Show-Command #1993
Comments
@corbob What do you mean by hyphens included? As in Show-Command ignores hyphens? This probably wouldn’t be considered a bug in Sort-Object but probably means we want to write our own sorting key function. |
I'd suggest something like this: internal class HyphenlessStringComparer : IComparer<string>
{
public int Compare(string a, string b)
{
if (a == b) { return 0; }
if (a == null) { return -1; }
if (b == null) { return 1; }
return a.Replace("-","").CompareTo(b.Replace("-",""));
}
}
...
var strs = new List<string>(){ "ConvertLkj", "Convert-Lkj", "ConvertTo-Lkj", "Convert-ToLkj" };
var comparer = new HyphenlessStringComparer();
strs.Sort(comparer); |
(That can be improved if we want, so that new strings aren't allocated for the comparison. But it's more logic to maintain) |
Oh I see that's not quite the sort order you're looking for. Can you describe formally what the sort order you want is? Is it just a system where In any case, the fix is pretty straightforward, we just need to specify exactly what sort order you expect. |
Formally the sort appears to be called Ordinal in the StringComparer class... It feels like it's probably a little more complicated for us because we have a void Main()
{
List<String> L = new List<String> {"aa", "a-a", "a-b", "ab", "ba", "bb", "b-a"};
L.Sort(StringComparer.Ordinal);
foreach (var s in L)
{
System.Console.WriteLine(s);
}
} I think the easiest way to outline the difference I'm seeing is this screenshot: Here you can see PowerShell on the left sorting the commands and you have So the point I'm now at, after stewing over it and trying to wrap my head around how to describe what it's doing and what I think it should be doing is this: does it matter that much? As I stated originally, the intent was to mimic |
The original intent behind Command Explorer was to mimic Show-Command. I noticed today that Show-Command sorts with the hyphens being of higher sort value than other letters. While the Sort-Object that we use in the Command Explorer seems to strip out the hyphens and sorts based on the result. So you'll see some
Convert-Toxxx
and thenConvertTo-yyy
followed byConvert-Tozzz
.System Details
This happens with Windows PowerShell 5.1 on Windows 10 1709 and insiders build 18898 as well as PowerShell Core 6.2. As such, I'm going to forego the vscode version and PSES.
Issue Description
Example of 4 made up functions:
When running Show-Command and then sorting for
lkj
:When running the equivalent Get-Command code
Get-Command *lkj | select name | Sort name | ogv
:I have also tested this in pwsh with the same behavior:
Expected Behaviour
Output commands in the order:
Actual Behaviour
Output commands in order:
The text was updated successfully, but these errors were encountered: