-1

I have an countries object that consists of a key with the country's abbreviation followed by an object with it's id, name, and defaultCurrency.

If I wanted to sort the object by the country 'name' what should I do?

var countries= {
    AD: { id: 'AD', name: 'Andorra', defaultCurrency: 'EUR' },
    AE: { id: 'AE', name: 'United Arab Emirates', defaultCurrency: 'AED' },
    BT: { id: 'BT', name: 'Bhutan', defaultCurrency: 'USD' },
    BW: { id: 'BW', name: 'Botswana', defaultCurrency: 'USD' },
    BZ: { id: 'BZ', name: 'Belize', defaultCurrency: 'BZD' },
    CA: { id: 'CA', name: 'Canada', defaultCurrency: 'CAD' },
    CC: { id: 'CC', name: 'Cocos (Keeling) Islands', defaultCurency: 'XAF' },
    CH: { id: 'CH', name: 'Switzerland', defaultCurrency: 'CHF' },
    CI: { id: 'CI', name: 'Côte d\'Ivoire', defaultCurrency: 'USD' },
    CK: { id: 'CK', name: 'Cook Islands', defaultCurrency: 'NZD' },
}

I tried converting it to an array using the following function but this leaves me with [object] with each key

var arr = [];
for (var key in countries) {
    if (countries.hasOwnProperty(key)) {
        arr.push(key + '=' + countries[key]);
    }
};

Note that I cannot just apply the sort using array, and need to convert the entire object into array first and then convert back into object.

alchuang
  • 3,487
  • 3
  • 34
  • 48
  • fyi, the semicolon after the `}` of the for loop is pointless - it'd better be placed after the `}` of the `var countries = {...}` – ThiefMaster Feb 10 '16 at 01:19
  • Possible duplicate of [Sorting an array of JavaScript objects](http://stackoverflow.com/questions/979256/sorting-an-array-of-javascript-objects) – Michael Ramos Feb 10 '16 at 01:45
  • How is this a duplicate? I've done my due diligence and searched on stackoverflow, read that question before and it didn't resolve my issue. That question involves an array. Mine is an object with key values and objects nested within.... – alchuang Feb 12 '16 at 00:25

3 Answers3

4

So I'd start with your conversion function to turn the obj into an array:

var arr = [];
for (var key in countries) {
  arr.push(countries[key]);
};

Then I would sort the array:

arr.sort(function(a, b) {
  return b.name - a.name;
});
sg.cc
  • 1,726
  • 19
  • 41
  • The are 2 errors: 1) the author asked to convert by country name (not by country ISO2 code). Hence your code will not work for e.g. Switherland with ISO2 code `ch`. 2) You created a new object wrapped around the original country object. Why? – Alexander Elgin Feb 10 '16 at 01:47
  • Thank you Alexander! I fixed the first one. The second one I did because I assumed, for some reason, that OP would want to keep both the code (in the `info` section) and the property key itself. Now that I see it, the two are identical. I'll fix that too. – sg.cc Feb 10 '16 at 01:50
  • Now the solution is almost identical to mine. – Alexander Elgin Feb 10 '16 at 01:51
2

Object fields cannot be sorted you should convert the object to an array first.

var countries = {
    AD: { id: 'AD', name: 'Andorra', defaultCurrency: 'EUR' },
    AE: { id: 'AE', name: 'United Arab Emirates', defaultCurrency: 'AED' },
    BT: { id: 'BT', name: 'Bhutan', defaultCurrency: 'USD' },
    BW: { id: 'BW', name: 'Botswana', defaultCurrency: 'USD' },
    BZ: { id: 'BZ', name: 'Belize', defaultCurrency: 'BZD' },
    CA: { id: 'CA', name: 'Canada', defaultCurrency: 'CAD' },
    CC: { id: 'CC', name: 'Cocos (Keeling) Islands', defaultCurency: 'XAF'},
    CH: { id: 'CH', name: 'Switzerland', defaultCurrency: 'CHF' },
    CI: { id: 'CI', name: 'Côte d\'Ivoire', defaultCurrency: 'USD' },
    CK: { id: 'CK', name: 'Cook Islands', defaultCurrency: 'NZD' },
};

var countryList = [];

for(var isoCode in countries) {
  if(countries.hasOwnProperty(isoCode)) {
    countryList.push(countries[isoCode]);
  }
}

countryList.sort(function(country1, country2) {
  return country1.name > country2.name;
});

document.write(JSON.stringify(countryList));
Alexander Elgin
  • 6,796
  • 4
  • 40
  • 50
0

You are push-ing strings into your array (by concatenating key, "+", and your country object), which is why each country is represented as "object"; if you want the actual country objects, just push those.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101