I am a newbie in programming, but I do have potential, I am using PowerShell to hone my skills, I have written a simple function to sort strings consisted of numbers humanly (i.e. in a order like 1 2 10 25 32 123), using the basic concept of padding, I would like to see it be expanded so that it can sort strings containing numbers like "a12b3 c14","a10b5 c12","a5b1 c2" alphanumerically(according to the value of numbers rather than single digits), it is currently above my level, for now, so I want someone more experienced than me to help me expand my code, I will post it below so you can review it:
function Sort-Num { PARAM ( [Parameter(ValueFromPipeline=$true)] [System.String]$String ) Process { $array = $String.Split( ) $padnum = @() $sorted = @() $numbers = @() $length=0 foreach ($arra in $array) { while ($arra.Length -gt $length) { $length=$arra.Length } } foreach ($arra in $array) { $padded = "{0:D$length}" -f [int64]$arra $padnum +=$padded } $sorted = $padnum | Sort-Object foreach ($sorte in $sorted) { $number=[int64]$sorte $numbers+=$number } $numbers } }
Edit: My idea is: first split the string by space and create an array of the substrings, then use regex match to find letters and numbers in the substrings, remember their respective position, creating an array of [PSCustomObject], then split the substrings in the first array into substrings of letters and numbers based on regex match recursively, foreach iteration of substrings of substrings create an [PSCustomObject] of the substring according to the order identified in second step, like: [PSCustomObject]@{letter="a";number=12;letter="b";number=3...} then find the longest number in each column and pad other numbers in the same column to the same length, and then sort the second array and then eliminate the leading zeroes and then rejoin the substrings of the second array and add them to a third array...
Maybe this sounds complex in natural language but this would be much simpler in code, I just haven't figured out how to implement my idea in code, yet...
Sort-Num -String "a12b3 c14"
(had overlooked that it's your future goal, sorry). Please be at least as thorough in formulating your questions. Your script does the same as[int64[]]"123456789 23456789 3456789 456789 56789 6789 789 89 9".Split() | Sort-Object
. And keep in mind that codereview isn't a free code-writing service so your demand "to help me expand my code" is definitely off-topic here!\$\endgroup\$$NaturalSort = {[regex]::Replace($_, '\d+', { $args[0].Value.PadLeft(20) })};@('a12b3', 'c14', 'a10b5', 'c12', 'a5b1', 'c2') | sort-object $NaturalSort
\$\endgroup\$