sorting - Alphabetize items in a JavaScript object? - Stack Overflow

Say I have an object that looks like this:countrylist:{regions:[{region: "Europe",countries:

Say I have an object that looks like this:

countrylist:{
        regions:[
            {
                region: "Europe",
                countries: [
                    {
                        "country":"Albania",
                        "href":"eu",
                        "locale":"en_al"
                    },
                    {
                        "country":"France",
                        "href":"eu",
                        "locale":"en_fr"
                    },
                    {
                        "country":"Ireland",
                        "href":"eu",
                        "locale":"en_ie"
                    }]      
                },
                    region: "Asia",
                    countries: [
                        {
                            "country":"China",
                            "href":"as",
                            "locale":"ch"
                        },
                        {
                            "country":"Japan",
                            "href":"as",
                            "locale":"jp"
                        },
                        {
                            "country":"Thailand",
                            "href":"as",
                            "locale":"th"
                        }]      
                    }
                 ]}

If you could see the whole object, you would see that it's grouped by region, and the countries within each region are sorted alphabetically. However, I need to populate a dropdown menu of all the countries, alphabetized, but not by region. What's the cleanest method to go about sorting these items?

I originally pushed the country field to an empty array and sorted that. However, I need to preserve the relationship between the country field and its corresponding href and locale fields.

Say I have an object that looks like this:

countrylist:{
        regions:[
            {
                region: "Europe",
                countries: [
                    {
                        "country":"Albania",
                        "href":"eu",
                        "locale":"en_al"
                    },
                    {
                        "country":"France",
                        "href":"eu",
                        "locale":"en_fr"
                    },
                    {
                        "country":"Ireland",
                        "href":"eu",
                        "locale":"en_ie"
                    }]      
                },
                    region: "Asia",
                    countries: [
                        {
                            "country":"China",
                            "href":"as",
                            "locale":"ch"
                        },
                        {
                            "country":"Japan",
                            "href":"as",
                            "locale":"jp"
                        },
                        {
                            "country":"Thailand",
                            "href":"as",
                            "locale":"th"
                        }]      
                    }
                 ]}

If you could see the whole object, you would see that it's grouped by region, and the countries within each region are sorted alphabetically. However, I need to populate a dropdown menu of all the countries, alphabetized, but not by region. What's the cleanest method to go about sorting these items?

I originally pushed the country field to an empty array and sorted that. However, I need to preserve the relationship between the country field and its corresponding href and locale fields.

Share Improve this question edited Oct 26, 2011 at 20:47 Paul Erdos asked Oct 26, 2011 at 20:36 Paul ErdosPaul Erdos 1,3752 gold badges23 silver badges49 bronze badges 2
  • I might be wrong but did you miss the opening brace of the second region? – pimvdb Commented Oct 26, 2011 at 20:38
  • I may have missed something while mocking up this dummy object. – Paul Erdos Commented Oct 26, 2011 at 20:46
Add a ment  | 

2 Answers 2

Reset to default 5

Initialize an empty array, then go through the regions and append all the countries to that array. When you're done, sort the array.

var countries = [];
for(var i = 0; i < countrylist.regions.length; i++)
    Array.prototype.push.apply(countries, countrylist.regions[i].countries);

countries.sort(function(a, b) {
    return a.country > b.country;
});

console.log(countries);

http://jsfiddle/Jehsb/

You need to walk your structure and create an array of country names. You can then sort the array, and you're done.

var arr = [];
for(var i = 0; i < countryList.regions.length; i++){
    var countries = countryList.regions[i].countries;
    for(var j = 0; j < countries.length; j++){
        arr.push(countries[j].country);
    }
}

arr.sort();

If you need the other information as well, what you'd need is a flat array of all country objects, and then apply a custom sort function:

var arr = [];
for(var i = 0; i < countryList.regions.length; i++){
    var countries = countryList.regions[i].countries;
    for(var j = 0; j < countries.length; j++){
        arr.push(countries[j]);
    }
}

arr.sort(function(xx,yy){ 
    return xx.country < yy.country ? -1 : 1;
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745580366a4634220.html

相关推荐

  • sorting - Alphabetize items in a JavaScript object? - Stack Overflow

    Say I have an object that looks like this:countrylist:{regions:[{region: "Europe",countries:

    23小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信