0

i have a json object.the object is like given below

{"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"}}

i want to sort this object with respect to rows and coloums in ascending order. like i want the resultant object to be like given below

{"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"}}

can some one help me out with this ??

Marc B
  • 356,200
  • 43
  • 426
  • 500
user2499644
  • 3
  • 1
  • 1
  • 2
  • 1
    Have you taken a look at this http://stackoverflow.com/questions/4222690/sorting-a-json-object-in-javascript ? – Kiran Ruth R Jul 10 '13 at 17:06
  • 6
    there's no such thing as a "json object". There's json strings. which can be DECODED into a native object. – Marc B Jul 10 '13 at 17:07
  • 1
    Have a look at [`[].sort`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort). Pass a function to compare the `column` values of each element in the `obj.route` array. There's an example of sorting an array of objects in the docs. – gen_Eric Jul 10 '13 at 17:08
  • do refer http://stackoverflow.com/questions/979256/sorting-an-array-of-javascript-objects – Nishant Jul 10 '13 at 17:09
  • Which gets precedence? Column or row? – crush Jul 10 '13 at 17:09
  • 2
    @MarcB You understood when OP said "json object" so it seems to have been a useful way to word it. What I hate is when someone says "I want to sort json" and you don't have any idea what they mean. – Lee Meador Jul 10 '13 at 17:09
  • There are some errors in the supplied JSON. – crush Jul 10 '13 at 17:17

2 Answers2

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

Given the supplied object:

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"}
    ]
};

You need to implement a sort function on the array like follows:

If column takes precedence over row, then you check column's value first. If it is equal, then you check the row's value.

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

If row takes precedence over column:

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

crush
  • 16,713
  • 9
  • 59
  • 100
  • @crush what is the purpose of those plus signs '+' you put before references? (e.g. `+a.row`, `+b.row`) Thanks in advance. – KiriSakow Aug 23 '17 at 18:23
  • 1
    @KiriSakow They are unary operators that convert the value into a numeric (in case it is a string originally). – crush Oct 13 '17 at 18:17