muledataweavemule-componentmule4

Sorting the string with comma separated numbers and assign sequence


I have a string value coming as "AH-0000006620, AH-0000006619, AH-0000006621", where I need to Remove the Prefix and the remaining numbers "0000006620, 0000006619, 0000006621" to be sorted in Ascending order and I want to store the sequence number for each of them like for 0000006619 value to be 1 for 0000006620 value to be 2 for 0000006621 value to be 3

So that when I send the info to target system, while iterating the array, for the first element if it is 0000006620 then I will pass the value as 2 for the other sequence tag. Please let me know how I can achieve this.


Solution

  • You can tweak this though this should serve as a good starting point to what you are looking for:

    Script

    %dw 2.0
    import * from dw::core::Strings
    output application/json
    var inp = "AH-0000006620,AH-0000006619,AH-0000006621"
    ---
    {((inp replace"AH-" with "") splitBy "," map ($ replace /^0+/ with "") orderBy $ map {
         (leftPad($ as String,10,0)) : ($$)
    })}
    

    You can do it this way as well.

    {((inp replace"AH-" with "") splitBy ","  orderBy $ as Number map {
         ($) : ($$)
    })}
    
    
    **Output**
    

    [ { "0000006619": 0 }, { "0000006620": 1 }, { "0000006621": 2 } ]