0

This is my array of object

[{"name":"Tania","country":"BR"},{"name":"Carl","country":"BR"},{"name":"Farl","country":"BR"},{"name":"Rany","country":"BR"},{"name":"Ron","country":"JP"},{"name":"Antony","country":"JP"}]

I want to sort this array by name and same country. This is the result

[{"name":"Carl","country":"BR"},{"name":"Farl","country":"BR"},{"name":"Rany","country":"BR"},{"name":"Tania","country":"BR"},{"name":"Antony","country":"JP"},{"name":"Ron","country":"JP"}]

To make the sort only for the name I make this, but in this case is not divided by country, how can I do this?

array.sort((f , n)=> {
        var nameF = f.name.toUpperCase();
        var nameN = n.name.toUpperCase();

        if(nameF < nameN) return -1;
        else if(nameF > nameN) return 1;
        else return 0
      });
Irrech
  • 909
  • 2
  • 9
  • 17
  • May a duplicate of: http://stackoverflow.com/questions/6913512/how-to-sort-an-array-of-objects-by-multiple-fields ? – caramba Aug 24 '16 at 08:41
  • Trick for this one : If all your country codes have exactly 2 characters, you could concatenate them with the name for sorting purposes. For example : `var nameF = f.country.toUpperCase() + f.name.toUpperCase();`. – Kewin Dousse Aug 24 '16 at 08:43
  • @Protectator I suppose yes, thks work for me – Irrech Aug 24 '16 at 08:53

0 Answers0