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 ??

7
  • 1
    Have you taken a look at this stackoverflow.com/questions/4222690/… ? Commented Jul 10, 2013 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. Commented Jul 10, 2013 at 17:07
  • 1
    Have a look at [].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. Commented Jul 10, 2013 at 17:08
  • do refer stackoverflow.com/questions/979256/… Commented Jul 10, 2013 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. Commented Jul 10, 2013 at 17:09

2 Answers 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);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks This Works Perfectly for Me :)
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

2 Comments

@crush what is the purpose of those plus signs '+' you put before references? (e.g. +a.row, +b.row) Thanks in advance.
@KiriSakow They are unary operators that convert the value into a numeric (in case it is a string originally).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.