0

我有一个 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"}}

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

4

2 回答 2

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);
于 2013-07-10T17:16:27.090 回答
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"}
    ]
};

您需要在数组上实现排序功能,如下所示:

如果列优先于行,则首先检查列的值。如果相等,则检查该行的值。

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;
});

小提琴演示

于 2013-07-10T17:14:54.587 回答