none
sort decimal values in desc orders of versions of artifacts receiving from third party response RRS feed

  • Question

  • I am not getting success to sort decimals in desc order

    $a="10.12.32.22/", "45.25.28.65/", "89.55.26.98/", "12.25.85.87/", "85.56.69.58/", "87.89.54.56/", "87.87.90.15/"
    
    $a format is $X.$Y.W.$Z/,   the answer is 89.55.26.98
    
    I have to sort the string such that first digit taken in  desc order of every split
    
    I am splitting every element in desc order further add it in first element get this answer
    
    $t=@()
    
    foreach ($d in $a){
    
           foreach ($f in $d){
    
          $e=$f.split('.')
    
          $t= $m+=$e[0] | sort -desc
    
        }
    
    }  
    
              If($t[0] -match $a.split(.)[0]) {
    
    echo $a
    
                }
    
    
    

    Monday, December 24, 2018 7:28 PM

All replies

  • You can try this:

    $a= @(
    '10.12.32.22',
    '45.25.28.65',
    '89.55.26.98',
    '12.25.85.87',
    '85.56.69.58',
    '87.89.54.56',
    '87.87.90.15')
    
    $a | Sort-Object -Descending -Property { [System.Version]$_ }

    Hope this helps.


    Andreas Baumgarten

    Thursday, December 27, 2018 11:20 AM
  • Thank you Andreas, I am getting versions in response of API in object format (system.object[])
    I used your code but unfortunately is it not sorted the values correctly
    I have got answer here : https://stackoverflow.com/questions/31597657/sorting-objects-by-multiple-properties

    where I have to put above array in below FORMAT
    CODE : 1

    $versionsObjects = [ordered]@{
        Major = 10
        Minor = 12
        Bugfix = 32
        Hotfix = 22
    },
    [ordered]@{
        Major = 45
        Minor = 25
        Bugfix = 28
        Hotfix = 65
    }
    

    here I have problem in loop, the output of $verArr is not look like in above FORMAT in ordered form

    CODE :2

    cls
    $verArr= @()
    $versionsObjects=[ordered]@{}
    $a= @(
    '10.12.32.22',
    '45.25.28.65',
    '89.55.26.98',
    '12.25.85.87',
    '85.56.69.58',
    '87.89.54.56',
    '87.87.90.15')
    
    foreach($c in $a) {
    $c=$c.Split('.')
        $versionsObjects = [ordered]@{
        Major = $c[0]
        Minor=$c[1]
        Bugfix=$c[2]
        hotfix=$c[3]
        }
        $verArr += New-Object -TypeName psobject $versionsObjects
    }
    echo $verArr
    

    The output is different as you see in below snap,it should be as per CODE : 1

    • Edited by Himanshu_Kulkarni Saturday, December 29, 2018 10:13 AM code format is wrong
    Saturday, December 29, 2018 10:04 AM
  • I don't get what is the requirement of your request. Maybe you can explain a little bit more what exactly do you need as the output and for what purpose.

    I thought you want to sort a list of IPs in a descending order. Which is done by the script I posted above.  


    Andreas Baumgarten

    Saturday, December 29, 2018 8:16 PM