最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I sort my JSON array? - Stack Overflow

programmeradmin2浏览0评论

How to output this JSON element in correct order by it's value?

var json = { 
    "message": {
        "90": "Adidas", 
        "2": "Armani", 
        "89": "Casio", 
        "1": "Diesel", 
        "72": "DKNY", 
        "88": "Fossil", 
        "4": "Hamilton", 
        "6": "Luminox", 
        "99": "Michael Kors", 
        "968": "Mont Blanc Pens", 
        "3": "Nixon", 
        "959": "Nooka", 
        "92": "Seiko", 
        "91": "Tendence", 
        "7": "Tissot" 
    } 
};

var str = '';
for (var i in json.message) {
    str += json.message[i]+'\n';
}
alert(str);

it alerts in the below order -

Diesel
Armani
Nixon
Hamilton
Luminox
DKNY
Fossil
Casio
Adidas
Tendence
Seiko
Michael Kors
Nooka
Mont Blanc Pens

But I want it in below order

Adidas
Armani
Casio
Diesel
DKNY
Fossil
Hamilton
Luminox
Michael Kors
Mont Blanc Pens
Nixon
Nooka
Seiko
Tendence
Tissot

Can any one suggest me what approach I should adopt to get in correct order?

Any help would be greatly appreciated!

How to output this JSON element in correct order by it's value?

var json = { 
    "message": {
        "90": "Adidas", 
        "2": "Armani", 
        "89": "Casio", 
        "1": "Diesel", 
        "72": "DKNY", 
        "88": "Fossil", 
        "4": "Hamilton", 
        "6": "Luminox", 
        "99": "Michael Kors", 
        "968": "Mont Blanc Pens", 
        "3": "Nixon", 
        "959": "Nooka", 
        "92": "Seiko", 
        "91": "Tendence", 
        "7": "Tissot" 
    } 
};

var str = '';
for (var i in json.message) {
    str += json.message[i]+'\n';
}
alert(str);

it alerts in the below order -

Diesel
Armani
Nixon
Hamilton
Luminox
DKNY
Fossil
Casio
Adidas
Tendence
Seiko
Michael Kors
Nooka
Mont Blanc Pens

But I want it in below order

Adidas
Armani
Casio
Diesel
DKNY
Fossil
Hamilton
Luminox
Michael Kors
Mont Blanc Pens
Nixon
Nooka
Seiko
Tendence
Tissot

Can any one suggest me what approach I should adopt to get in correct order?

Any help would be greatly appreciated!

Share Improve this question edited Jan 19, 2012 at 10:39 Rory McCrossan 338k41 gold badges320 silver badges351 bronze badges asked Jan 19, 2012 at 10:26 Vikas NaranjeVikas Naranje 2,4026 gold badges31 silver badges41 bronze badges 1
  • Objects (not array, it's only PHP that uses things like associative arrays), do not have their keys in any order, although most JS VM's do preserve the order they were added in. But, since this can not be relied on, you need to generate an array of the values only, and then simply sort this (as other answers shows). – Sean Kinsey Commented Nov 9, 2012 at 5:49
Add a ment  | 

5 Answers 5

Reset to default 8

Assuming you want the elements listed in alphabetical order by their value:

var values = [];
for(var i in json.message) {
   values.push(json.message[i]);
}
var str = values.sort().join('\n');

Update

To form an array of key-value pairs ordered by their (string) value:

var values = [];
for(var i in json.message) {
   values.push({ key: i, value: json.message[i] });
}
values.sort(function(a, b) { return a.value.localeCompare(b.value); });
var str = values.map(function (kvp) { return kvp.value; }).join('\n');
var message = object.message;
var brands = Object.keys(message)
               .map(function(key) { return message[key]; })
               .sort();

alert(brands.join('\n'));

Add a leading 0 to each index. For example:

var json = { 
    "message": {
        "090": "Adidas", 
        "02": "Armani", 
        "089": "Casio", 
        "01": "Diesel", 
        ...

using JS library undescore.js is pretty easy, just do:

json.message=_.sortBy(json.message,function(i){return i;})

You can sort it by this way, this is a sort basied on values :

function sortJsonMap(data){

var values = [];
var keys = [];

for(var i in data) {
   keys.push(i);
   values.push(data[i]);                     
}
values.sort();  
var map = new Object();
for(var i in values) {
     map[keys[i]]=values[i];
}
return map;

}

发布评论

评论列表(0)

  1. 暂无评论