PowerShell Sort Array

My array spits it out.

a10 a11 a12 a6 a7 a8 a9 

Any short / simple code to fix it:

 a6 a7 a8 a9 a10 a11 a12 
+4
source share
3 answers

You can sort by expression, take everything after the first letter and apply it to an integer:

 $array | sort { [int]$_.substring(1)} 

You can also make the solution more universal by removing any unsigned characters:

 $array | sort { [int]($_ -replace '\D')} 
+10
source

The easiest way in this case is to reset all numbers and use them to sort:

 $a | sort { [Regex]::Replace($_, '\d+', { $args[0].Value.PadLeft(10, '0') }) } 
+5
source

These are hexadecimal values, right ?; -)

 $array | sort {[convert]::toint32("$_",16)} 
+2
source

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


All Articles