Sort by Filename Numerically

Tagged: 

This topic contains 5 replies, has 4 voices, and was last updated by Profile photo of Ron Ron 2 weeks, 3 days ago.

  • Author
    Posts
  • #58145

    Hi Guys,

    I've just joined the forum and the past week (in my holidays) I've been watching Tutorials on Youtube trying to teach myself PowerShell.

    I don't understand much of it at all at the moment but I'm running into problems immediately. I have a list of Directories From -12 to +30.

    -12
    -11
    And so on to
    +29
    +30

    The closest I've got so far is

    I'm trying to arrange these numerically but I understand that PowerShell 4.00 indexes using ASCII. There doesn't seem to be a great deal out there on this; there are many articles but none of any real sustenance. I've tried various commands such as gci | Sort – Descending and Filter with different parameters etc.
    Suprised there isn't more quality info out there on this or a better solution to date. There was an interesting debating article saying that we're failing to have not addressed this yet as humans are working to accomodate the computer's needs here.

    gci *slab* | sort -Descending
    Directory: C:\Users\Precise Formwork Ltd\Desktop\Dals BBS Revolution\Block A 2nd Crack PDF2XL
    Mode LastWriteTime Length Name
    —- ————- —— —-
    d—- 18/11/2016 15:05 Slabs
    d—- 18/11/2016 13:57 Lv-04 Slab
    d—- 18/11/2016 13:55 Lv-03 Slab
    d—- 18/11/2016 13:57 Lv-02 Slab
    d—- 17/11/2016 17:05 Lv-01 Slab
    d—- 17/11/2016 12:39 Lv0 Slab
    d—- 17/11/2016 12:39 Lv+05 Slab
    d—- 17/11/2016 12:39 Lv+04 Slab
    d—- 17/11/2016 12:39 Lv+03 Slab
    d—- 17/11/2016 12:39 Lv+02 Slab
    d—- 18/11/2016 14:19 Lv+01 Slab

  • #58180
    Profile photo of Matt Howard
    Matt Howard
    Participant

    Try this:

    gci *slab* | sort @{Expression={if ($_.Name -match "\+") { $_.Name }};Ascending=$true},Name -Descending
    

    I tested it and I think it gives the result you are looking for. Basically, you are telling Sort-Object that if the name contains "+" to sort it Ascending order, but everything else sort descending.

    For reference, check out this article: https://blogs.msdn.microsoft.com/powershell/2006/04/25/working-with-sort-object-cmdlet

  • #58193

    Thanks Matt, that's some tricky code there; don't think I'll ever get to that sort of level. That link looks pretty relevant; having a look through it now.

    It's printing out like this atm, thanks again.

    —- ————- —— —-
    d—- 18/11/2016 17:51 Lv-9 Slab
    d—- 18/11/2016 17:51 Lv-8 Slab
    d—- 18/11/2016 17:51 Lv-7 Slab
    d—- 18/11/2016 17:51 Lv-6 Slab
    d—- 18/11/2016 17:52 Lv-4 Slab
    d—- 18/11/2016 17:52 Lv-3 Slab
    d—- 18/11/2016 17:52 Lv-2 Slab
    d—- 18/11/2016 17:51 Lv-12 Slab
    d—- 18/11/2016 17:51 Lv-11 Slab
    d—- 18/11/2016 17:51 Lv-10 Slab
    d—- 18/11/2016 17:52 Lv-1 Slab
    d—- 18/11/2016 17:53 Lv0 Slab
    d—- 18/11/2016 17:53 Lv+1 Slab
    d—- 18/11/2016 17:53 Lv+10 Slab
    d—- 18/11/2016 17:53 Lv+11 Slab
    d—- 18/11/2016 17:53 Lv+12 Slab
    d—- 18/11/2016 17:53 Lv+13 Slab
    d—- 18/11/2016 17:53 Lv+14 Slab
    d—- 18/11/2016 17:53 Lv+15 Slab
    d—- 18/11/2016 17:53 Lv+16 Slab
    d—- 18/11/2016 17:53 Lv+17 Slab
    d—- 18/11/2016 17:53 Lv+18 Slab
    d—- 18/11/2016 17:53 Lv+19 Slab
    d—- 18/11/2016 17:53 Lv+2 Slab
    d—- 18/11/2016 17:53 Lv+20 Slab
    d—- 18/11/2016 17:53 Lv+21 Slab
    d—- 18/11/2016 17:53 Lv+22 Slab
    d—- 18/11/2016 17:53 Lv+24 Slab
    d—- 18/11/2016 17:54 Lv+25 Slab
    d—- 18/11/2016 17:54 Lv+26 Slab
    d—- 18/11/2016 17:54 Lv+27 Slab
    d—- 18/11/2016 17:54 Lv+28 Slab
    d—- 18/11/2016 17:54 Lv+29 Slab
    d—- 18/11/2016 17:53 Lv+3 Slab
    d—- 18/11/2016 17:54 Lv+30 Slab
    d—- 18/11/2016 17:53 Lv+4 Slab
    d—- 18/11/2016 17:51 Lv+5 Slab
    d—- 18/11/2016 17:53 Lv+6 Slab
    d—- 18/11/2016 17:53 Lv+7 Slab
    d—- 18/11/2016 17:53 Lv+8 Slab
    d—- 18/11/2016 17:53 Lv+9 Slab")

  • #58201
    Profile photo of Matt Howard
    Matt Howard
    Participant

    Ah, I tested with double-digits for all folder names (Lv-01 instead of Lv-1) based on your original post.

    • #58202
      Profile photo of Olaf Soyk
      Olaf Soyk
      Participant

      Another approach could be this:

      Get-ChildItem -Path 'C:\Users\Precise Formwork Ltd\Desktop\Dals BBS Revolution\Block A 2nd Crack PDF2XL' | 
          Select-Object -Property Name,@{Name='Index';Expression={$_.Name  -match 'lv([\+\-]?\d+)\s+' | Out-Null ; [INT]$matches[1] } } | 
          Sort-Object -Property Index | 
          Select-Object -Property Name
      

      The advantage is that the Sort-Object has an integer to sort.

  • #58207
    Profile photo of Ron
    Ron
    Participant

    Late to the party, but came up with something similar to Olaf. I went with floating point and tested names without a number. They'll sort as zero and use the original name as a tie breaker.

    $test=@'
    Name
    Lv0 Slab
    Lv+1 Slab
    Lv+1.1 Slab
    Lv+1.01 Slab
    Lv-10 Slab
    Lv-1 Slab
    Lv+10 Slab
    LvX Slab
    '@ | convertfrom-csv
    
    $test|select *,@{Name="Float";Expression={[float][regex]::Match($_.Name,"[+-]?(?:[0-9]*[.])?[0-9]+").Value}}|sort float,name|Select * -exclude Float
    
    Name
    ----
    Lv-10 Slab
    Lv-1 Slab
    Lv0 Slab
    LvX Slab
    Lv+1 Slab
    Lv+1.01 Slab
    Lv+1.1 Slab
    Lv+10 Slab
    

You must be logged in to reply to this topic.