• "Australia #11 comes after Australia #100" well, that's correct, if it's ordered lexicographically – Federico klez Culloca Feb 21 '18 at 09:43
  • May be that is due to, you get data in an object format.... You should get data in array.... show us data you get from server – Kiran Shinde Feb 21 '18 at 09:43
  • @Nihal updated with image – Ryan Peters Feb 21 '18 at 09:43
  • 1 Answers1

    1

    The list is sorted alphabetically, so "#11" comes after "#109".

    To fix this, you can write your own sort function to consider only the numerical part instead of the whole string. That way it gets sorted numerically and not alphabetically. Here's a quick ES6 solution.

    let countryList  =    [/*list here*/];
    
    countryList      =    countryList.sort( (eachLine, prevLine) => eachLine.split("#")[1].split(" ")[0] - prevLine.split("#")[1].split(" ")[0] );
    

    This simply sorts it based on the number part of the string alone and ignores the rest.

    hashedram
    • 813
    • 7
    • 14