javascript - Sorting a JSON object with an id - Stack Overflow

Goodmorning, I am having a problem figuring out how to sort the following JSON{"135": {"

Goodmorning, I am having a problem figuring out how to sort the following JSON

{
    "135": {"name": "marco", "title": "foo"},
    "223": {"name": "wilco", "title": "bar"},
    "322": {"name": "enola", "title": "baz"}
}

I wish to sort by name (and sometimes, possible title. However the plete line should be sorted. Should I convert this into an proper array first?

Thanks!

Goodmorning, I am having a problem figuring out how to sort the following JSON

{
    "135": {"name": "marco", "title": "foo"},
    "223": {"name": "wilco", "title": "bar"},
    "322": {"name": "enola", "title": "baz"}
}

I wish to sort by name (and sometimes, possible title. However the plete line should be sorted. Should I convert this into an proper array first?

Thanks!

Share Improve this question asked Oct 10, 2012 at 7:34 MarcoMarco 2,4633 gold badges23 silver badges21 bronze badges 1
  • You can never sort an object. You need to convert this into an array. – Jashwant Commented Oct 10, 2012 at 7:46
Add a ment  | 

4 Answers 4

Reset to default 3

Should I convert this into an proper array first?

Yes, since objects don't guarantee order. Additionally, you'd have to modify the structure a bit:

[
    {"id":"135", "name": "marco", "title": "foo"},
    {"id":"223", "name": "wilco", "title": "bar"},
    {"id":"322", "name": "enola", "title": "baz"}
]

As for sorting algorithms, there are a lot on the web, especially this one.

You can never sort an object. You need to convert this into an array.

For this particular problem if you cannot change the object into array.

You can follow this,

var obj = { 
    "223": {"name": "wilco", "title": "bar"},
    "322": {"name": "enola", "title": "baz"},
    "135": {"name": "marco", "title": "foo"}
};

var arr = [];
for(var i in obj) {
    var o = {};
    if(obj.hasOwnProperty(i)) {
       o.id = i;
       o.name = obj[i].name;
       o.title = obj[i].title;
       arr.push(o); 
    }        
} 
arr.sort(function (a,b) {
   a.id - b.id;
});
console.log(arr);

​Demo

you may want to lookat underscore which makes working with objects/arrays easier for you. Yeap, you need to make it into an array, so that you can even have a reliable order/sort it.

var array=[];
for (var i in obj){
    if (obj.hasOwnProperty(i)){
        obj[i].id=i;
        array.push(obj[i]);
    }
}
var fieldToSort="id"; //or "title" or "name"
array.sort(function(a,b){
    return a[fieldToSort] - b[fieldToSort];
});

You can try to sort your object props without creating an array, but it will cost you more lines of code to write, and you have to watch for your properties, so they didn't inherited from other objects. So you have to use for in loop:

for (var prop in obj) {
  if(obj.hasOwnProperty(prop)) {
    //perform sorting...
  }
}

Or you can transform your object into an array and sort data via standard js array sorting function:

function sortObj(obj) {
  var objToArr = [],
  prop,
  sortedObj = {};
  for (prop in obj) {
    if (obj.hasOwnProperty(prop)) {
        objToArr.push([prop, obj[prop]]);
    }
   }

 objToArr.sort(function(a, b) { return a[0] - b[0]; });
 objToArr.forEach(function (val, key) {
   sortedObj[val[0]] = sortedObj[val[1]];
 });
 return sortedObj;
}

You can adopt this function for more plex objects, for example when one property of object contains another object, etc.

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

相关推荐

  • javascript - Sorting a JSON object with an id - Stack Overflow

    Goodmorning, I am having a problem figuring out how to sort the following JSON{"135": {"

    8天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信