0
\$\begingroup\$

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...

\$\endgroup\$
7
  • \$\begingroup\$Your question seems to be off-topic here (syntax errors). Code Review aims to help improve working code; ask on Stack Overflow instead.\$\endgroup\$
    – JosefZ
    CommentedDec 5, 2020 at 10:48
  • \$\begingroup\$@JosefZ, what sort of syntax errors? I have tested my code on Windows 10 x64 PowerShell PSCore 7.1 and it is really working, I wouldn't post it if I didn't test it, maybe you used a lower version of powershell or windows? I don't guarantee my code will work on lower versions of PowerShell...\$\endgroup\$CommentedDec 5, 2020 at 10:57
  • \$\begingroup\$Edit: for those who even greener than me, here is a simple guide on how to use my code; First, make sure your operating system is Windows 7 and above, PowerShell 5.1 and above, PSCore6 and above prefered, I recommend Windows 10 x64 and PowerShell 7.1 x64 because they are what I used when I tested my code, these thing are extremely important because the softwares aren't forward compatible, when the outdated softwares were written the newer syntaxes of PowerShell programming language haven't been invented, so there might be syntax errors as the commenter noticed.\$\endgroup\$CommentedDec 5, 2020 at 12:22
  • 1
    \$\begingroup\$🤣 Thank you for thorough guide 😂. Tried 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\$
    – JosefZ
    CommentedDec 5, 2020 at 18:05
  • 1
    \$\begingroup\$A natural sort trick picked up here applied to your inputs: $NaturalSort = {[regex]::Replace($_, '\d+', { $args[0].Value.PadLeft(20) })};@('a12b3', 'c14', 'a10b5', 'c12', 'a5b1', 'c2') | sort-object $NaturalSort\$\endgroup\$CommentedDec 8, 2020 at 7:50

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.