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

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 
+4
source share
2 answers

$var | sort -Unique COUNT $var | sort -Unique COUNT matches: $var | sort -Unique -Property COUNT $var | sort -Unique -Property COUNT

So what sorting does is look for the "COUNT" property for 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 has the "COUNT" property, sorting considers them all the same, and therefore no one is unique and returns one of the elements. The hint came from trying:

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

this led to result "c".

Measure is your friend here:

 $var |sort -Unique |measure 

That should do the trick.

+6
source

I'm not sure why he does this, but -sort does not have a count parameter.

I think you can be as follows:

 $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 
+3
source

Source: https://habr.com/ru/post/1397144/


All Articles