2017-11-23 8 views
0

私は以下のようなオブジェクトを持っています。これをjavascriptのA-Zからソートしたいのですがどうすればソートできますか?配列内のオブジェクトをA-Zからソートする

[ 
    {"country" :"NG", "code" : "00234"}, 
    {"country" :"NZ", "code" : "0064"}, 
    {"country" :"NP", "code" : "00977"}, 
    {"country" :"NR", "code" : "00674"}, 
    {"country" :"NU", "code" : "00683"}, 
    {"country" :"CK", "code" : "00682"}, 
    {"country" :"CI", "code" : "00225"}, 
    {"country" :"CH", "code" : "0041"}, 
    {"country" :"CO", "code" : "0057"}, 
    {"country" :"CN", "code" : "0086"}, 
    {"country" :"CM", "code" : "00237"}, 
    {"country" :"CL", "code" : "0056"}, 
    {"country" :"CC", "code" : "0061"}, 
    {"country" :"CA", "code" : "001"}, 
    {"country" :"CG", "code" : "00242"}, 
] 

答えて

1

array.sortを使用できます。 JavaScriptで

var myarray = [ 
 
    {"country" :"NG", "code" : "00234"}, 
 
    {"country" :"NZ", "code" : "0064"}, 
 
    {"country" :"NP", "code" : "00977"}, 
 
    {"country" :"NR", "code" : "00674"}, 
 
    {"country" :"NU", "code" : "00683"}, 
 
    {"country" :"CK", "code" : "00682"}, 
 
    {"country" :"CI", "code" : "00225"}, 
 
    {"country" :"CH", "code" : "0041"}, 
 
    {"country" :"CO", "code" : "0057"}, 
 
    {"country" :"CN", "code" : "0086"}, 
 
    {"country" :"CM", "code" : "00237"}, 
 
    {"country" :"CL", "code" : "0056"}, 
 
    {"country" :"CC", "code" : "0061"}, 
 
    {"country" :"CA", "code" : "001"}, 
 
    {"country" :"CG", "code" : "00242"}, 
 
]; 
 

 
var sorted = myarray.sort((a, b) => a.country.localeCompare(b.country)); 
 

 
console.log(sorted);

+0

アレイが目的です。 – trincot

+0

ええ!合意した!おそらくOPはそれを意味しない – Sajeetharan

関連する問題