如何对json对象进行排序?

时间:2013-07-10 17:04:43

标签: jquery json

我有一个json对象。该对象如下所示

{"route":[
{"match":"true","column":"10","fare":"500.0","source":"false","length":"1","name":"41","row":"4","width":"1","zIndex":"0"},
{"match":"true","column":"9","fare":"500.0","source":"false","length":"1","name":"37","row":"3","width":"1","zIndex":"0"},
{"match":"true","column":"8","fare":"500.0","source":"false","length":"1","name":"33","row":"2","width":"1","zIndex":"0"},
{"match":"true","column":"7","fare":"500.0","source":"false","length":"1","name":"29","row":"1","width":"1","zIndex":"0"}}
{"match":"true","column":"6","fare":"500.0","source":"false","length":"1","name":"29","row":"0","width":"1","zIndex":"0"}}

我想按行和列的升序排序这个对象。就像我希望结果对象如下所示

{"route":[
{"match":"true","column":"6","fare":"500.0","source":"false","length":"1","name":"41","row":"0","width":"1","zIndex":"0"},
{"match":"true","column":"7","fare":"500.0","source":"false","length":"1","name":"37","row":"1","width":"1","zIndex":"0"},
{"match":"true","column":"8","fare":"500.0","source":"false","length":"1","name":"33","row":"2","width":"1","zIndex":"0"},
{"match":"true","column":"9","fare":"500.0","source":"false","length":"1","name":"29","row":"3","width":"1","zIndex":"0"}}
{"match":"true","column":"10","fare":"500.0","source":"false","length":"1","name":"29","row":"4","width":"1","zIndex":"0"}}

有人可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:2)

var o = {"route":[
{"match":"true","column":"10","fare":"500.0","source":"false","length":"1","name":"41","row":"4","width":"1","zIndex":"0"},
{"match":"true","column":"9","fare":"500.0","source":"false","length":"1","name":"37","row":"3","width":"1","zIndex":"0"},
{"match":"true","column":"8","fare":"500.0","source":"false","length":"1","name":"33","row":"2","width":"1","zIndex":"0"},
{"match":"true","column":"7","fare":"500.0","source":"false","length":"1","name":"29","row":"1","width":"1","zIndex":"0"},
{"match":"true","column":"6","fare":"500.0","source":"false","length":"1","name":"29","row":"0","width":"1","zIndex":"0"}]};

var r = o.route;
// sort
var sorted = r.sort(function (a, b) {
    a = parseInt(a.column, 10);
    b = parseInt(b.column, 10);
    return a < b ? -1 : a > b ? 1 : 0;
});
console.log(sorted);

答案 1 :(得分:1)

给出提供的对象:

var obj = {
    "route" : [
        {"match":"true","column":"10","fare":"500.0","source":"false","length":"1","name":"41","row":"4","width":"1","zIndex":"0"},
        {"match":"true","column":"9","fare":"500.0","source":"false","length":"1","name":"37","row":"3","width":"1","zIndex":"0"},
        {"match":"true","column":"8","fare":"500.0","source":"false","length":"1","name":"33","row":"2","width":"1","zIndex":"0"},
        {"match":"true","column":"7","fare":"500.0","source":"false","length":"1","name":"29","row":"1","width":"1","zIndex":"0"},
        {"match":"true","column":"6","fare":"500.0","source":"false","length":"1","name":"29","row":"0","width":"1","zIndex":"0"}
    ]
};

您需要在数组上实现一个sort函数,如下所示:

如果列优先于行,则首先检查列的值。如果它相等,那么你检查行的值。

obj.route.sort(function(a, b) {
    if (a.column == b.column) {
        return a.row == b.row ? 0 : +a.row > +b.row ? 1 : -1;
    }

    return +a.column > +b.column ? 1 : -1;
});

如果行优先于列:

obj.route.sort(function(a, b) {
    if (a.row == b.row) {
        return a.column == b.column ? 0 : +a.column > +b.column ? 1 : -1;
    }

    return +a.row > +b.row ? 1 : -1;
});

FIDDLE DEMO