Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get this output in PowerShell sort -unique Count

Tags:

powershell

I typed the following command to find out how many unique objects there were and it gave me 5. I don't know why this gives 5.

> $var = @(2,4,2,5,3,6,34,6,3,6,4,6,3,5,5,353,5343,5,3,56,34)  
>$var | sort -Unique  
2  
3  
4  
5  
6  
34  
56  
353  
5343  
>$var | sort -Unique Count  
5
like image 368
Mitul Avatar asked Dec 22 '22 02:12

Mitul


2 Answers

$var | sort -Unique COUNT is the same as: $var | sort -Unique -Property COUNT

So what sort is doing is looking for the "COUNT" property on each of the elements in the array to determine whether they are unique or not. You can see how this works if you do the following:

GPS sv* | sort -Unique ID
GPS sv* | sort -Unique Name

Since none of the objects have a "COUNT" property, sort sees them all as the same and therefore none are unique and it is returning one of the elements. The clue came from trying the following:

$var = $("a", "b", "c", "b")
$var | sort -Unique count

this produced the result "c".

Measure is your friend here:

$var |sort -Unique |measure

That should do the trick.

like image 74
Jeffrey Snover - MSFT Avatar answered Jan 06 '23 11:01

Jeffrey Snover - MSFT


I'm not sure why it's doing that either, but -sort doesn't have a count parameter.

I think what you might be after is:

$var = @(2,4,2,5,3,6,34,6,3,6,4,6,3,5,5,353,5343,5,3,56,34)  
($var | sort -Unique).count  
like image 26
mjolinor Avatar answered Jan 06 '23 11:01

mjolinor