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.