Skip to content

Instantly share code, notes, and snippets.

@AlexKalinin
Last active November 17, 2016 07:07
Show Gist options
  • Save AlexKalinin/ced26ed230ad1f79e4a394739f2214e3 to your computer and use it in GitHub Desktop.
Save AlexKalinin/ced26ed230ad1f79e4a394739f2214e3 to your computer and use it in GitHub Desktop.
Sort of nested array items (or why sql is better :) )
var obj = JSON.parse('[[{"sort_index":0,"value":0,"title":"index"},{"sort_index":1,"value":"ezxczxcst","title":"tags"},{"sort_index":2,"value":"Rhozxczxcda","title":"name"},{"sort_index":3,"value":"Onezxcczxil","title":"lastName"},{"sort_index":4,"value":"A582978zxczxc6d114cb16e52a8959b","title":"passport"},{"sort_index":5,"value":"373 Kensizxcczx ngton Walk, Independence","title":"adress"}],[{"sort_index":0,"value":0,"title":"index"},{"sort_index":1,"value":"vbbest","title":"tags"},{"sort_index":2,"value":"zx Rhoda","title":"name"},{"sort_index":3,"value":"Oneil","title":"lastName"},{"sort_index":4,"value":"A582zxc9786d114cb16e52a8959b","title":"passport"},{"sort_index":5,"value":"373 Kenzxczxcsington Walk, Independence","title":"adress"}],[{"sort_index":0,"value":0,"title":"index"},{"sort_index":1,"value":"zxc zxcest","title":"tags"},{"sort_index":2,"value":"zxczxcRhoda","title":"name"},{"sort_index":3,"value":"Ozxczxcneil","title":"lastName"},{"sort_index":4,"value":"zxczxcA5829786d114cb16e52a8959b","title":"passport"},{"sort_index":5,"value":"3cvb73 Kensington Walk, Independence","title":"adress"}],[{"sort_index":0,"value":0,"title":"index"},{"sort_index":1,"value":"asdasdest","title":"tags"},{"sort_index":2,"value":"sdfgdfRhoda","title":"name"},{"sort_index":3,"value":"dfgaOneil","title":"lastName"},{"sort_index":4,"value":"sdfsfsd","title":"passport"},{"sort_index":5,"value":"sdfsdf373 Kensington Walk, Independence","title":"adress"}],[{"sort_index":0,"value":0,"title":"index"},{"sort_index":1,"value":"sdffqweest","title":"tags"},{"sort_index":2,"value":"Rqwehoda","title":"name"},{"sort_index":3,"value":"qweOneil","title":"lastName"},{"sort_index":4,"value":"qwe","title":"passport"},{"sort_index":5,"value":"373 qwe Walk, Independence","title":"adress"}],[{"sort_index":0,"value":0,"title":"index"},{"sort_index":1,"value":"qweqwe","title":"tags"},{"sort_index":2,"value":"dfgd","title":"name"},{"sort_index":3,"value":"dfgdfg","title":"lastName"},{"sort_index":4,"value":"A58297dfg86d114cb16e52a8959b","title":"passport"},{"sort_index":5,"value":"373 Kensington Walk, Independence","title":"adress"}],[{"sort_index":0,"value":0,"title":"index"},{"sort_index":1,"value":"est","title":"tags"},{"sort_index":2,"value":"Rhoda","title":"name"},{"sort_index":3,"value":"Oneil","title":"lastName"},{"sort_index":4,"value":"A5829786d114cb16e52a8959b","title":"passport"},{"sort_index":5,"value":"373 Kensington Walk, Independence","title":"adress"}],[{"sort_index":0,"value":0,"title":"index"},{"sort_index":1,"value":"est","title":"tags"},{"sort_index":2,"value":"Rhoda","title":"name"},{"sort_index":3,"value":"Oneil","title":"lastName"},{"sort_index":4,"value":"A5829786d114cb16e52a8959b","title":"passport"},{"sort_index":5,"value":"373 Kensington Walk, Independence","title":"adress"}],[{"sort_index":0,"value":0,"title":"index"},{"sort_index":1,"value":"est","title":"tags"},{"sort_index":2,"value":"Rhoda","title":"name"},{"sort_index":3,"value":"Oneil","title":"lastName"},{"sort_index":4,"value":"A5829786d114cb16e52a8959b","title":"passport"},{"sort_index":5,"value":"373 Kensington Walk, Independence","title":"adress"}],[{"sort_index":0,"value":0,"title":"index"},{"sort_index":1,"value":"est","title":"tags"},{"sort_index":2,"value":"Rhoda","title":"name"},{"sort_index":3,"value":"Oneil","title":"lastName"},{"sort_index":4,"value":"A5829786d114cb16e52a8959b","title":"passport"},{"sort_index":5,"value":"373 Kensington Walk, Independence","title":"adress"}]]')
function simplefy(arr, field){
for(var i = 0; i < arr.length; i++){
if(arr[i].title == field){
return arr[i].value;
}
}
}
var sorted = obj.sort(function(a, b){
var field = 'tags';
var direction = -1;
return simplefy(a, field) > simplefy(b, field) ? 1 * direction : -1 * direction;
});
[
[
{
"sort_index": 0,
"value": 0,
"title": "index"
},
{
"sort_index": 1,
"value": "ezxczxcst",
"title": "tags"
},
{
"sort_index": 2,
"value": "Rhozxczxcda",
"title": "name"
},
{
"sort_index": 3,
"value": "Onezxcczxil",
"title": "lastName"
},
{
"sort_index": 4,
"value": "A582978zxczxc6d114cb16e52a8959b",
"title": "passport"
},
{
"sort_index": 5,
"value": "373 Kensizxcczx ngton Walk, Independence",
"title": "adress"
}
],
[
{
"sort_index": 0,
"value": 0,
"title": "index"
},
{
"sort_index": 1,
"value": "vbbest",
"title": "tags"
},
{
"sort_index": 2,
"value": "zx Rhoda",
"title": "name"
},
{
"sort_index": 3,
"value": "Oneil",
"title": "lastName"
},
{
"sort_index": 4,
"value": "A582zxc9786d114cb16e52a8959b",
"title": "passport"
},
{
"sort_index": 5,
"value": "373 Kenzxczxcsington Walk, Independence",
"title": "adress"
}
],
[
{
"sort_index": 0,
"value": 0,
"title": "index"
},
{
"sort_index": 1,
"value": "zxc zxcest",
"title": "tags"
},
{
"sort_index": 2,
"value": "zxczxcRhoda",
"title": "name"
},
{
"sort_index": 3,
"value": "Ozxczxcneil",
"title": "lastName"
},
{
"sort_index": 4,
"value": "zxczxcA5829786d114cb16e52a8959b",
"title": "passport"
},
{
"sort_index": 5,
"value": "3cvb73 Kensington Walk, Independence",
"title": "adress"
}
],
[
{
"sort_index": 0,
"value": 0,
"title": "index"
},
{
"sort_index": 1,
"value": "asdasdest",
"title": "tags"
},
{
"sort_index": 2,
"value": "sdfgdfRhoda",
"title": "name"
},
{
"sort_index": 3,
"value": "dfgaOneil",
"title": "lastName"
},
{
"sort_index": 4,
"value": "sdfsfsd",
"title": "passport"
},
{
"sort_index": 5,
"value": "sdfsdf373 Kensington Walk, Independence",
"title": "adress"
}
],
[
{
"sort_index": 0,
"value": 0,
"title": "index"
},
{
"sort_index": 1,
"value": "sdffqweest",
"title": "tags"
},
{
"sort_index": 2,
"value": "Rqwehoda",
"title": "name"
},
{
"sort_index": 3,
"value": "qweOneil",
"title": "lastName"
},
{
"sort_index": 4,
"value": "qwe",
"title": "passport"
},
{
"sort_index": 5,
"value": "373 qwe Walk, Independence",
"title": "adress"
}
],
[
{
"sort_index": 0,
"value": 0,
"title": "index"
},
{
"sort_index": 1,
"value": "qweqwe",
"title": "tags"
},
{
"sort_index": 2,
"value": "dfgd",
"title": "name"
},
{
"sort_index": 3,
"value": "dfgdfg",
"title": "lastName"
},
{
"sort_index": 4,
"value": "A58297dfg86d114cb16e52a8959b",
"title": "passport"
},
{
"sort_index": 5,
"value": "373 Kensington Walk, Independence",
"title": "adress"
}
],
[
{
"sort_index": 0,
"value": 0,
"title": "index"
},
{
"sort_index": 1,
"value": "est",
"title": "tags"
},
{
"sort_index": 2,
"value": "Rhoda",
"title": "name"
},
{
"sort_index": 3,
"value": "Oneil",
"title": "lastName"
},
{
"sort_index": 4,
"value": "A5829786d114cb16e52a8959b",
"title": "passport"
},
{
"sort_index": 5,
"value": "373 Kensington Walk, Independence",
"title": "adress"
}
],
[
{
"sort_index": 0,
"value": 0,
"title": "index"
},
{
"sort_index": 1,
"value": "est",
"title": "tags"
},
{
"sort_index": 2,
"value": "Rhoda",
"title": "name"
},
{
"sort_index": 3,
"value": "Oneil",
"title": "lastName"
},
{
"sort_index": 4,
"value": "A5829786d114cb16e52a8959b",
"title": "passport"
},
{
"sort_index": 5,
"value": "373 Kensington Walk, Independence",
"title": "adress"
}
],
[
{
"sort_index": 0,
"value": 0,
"title": "index"
},
{
"sort_index": 1,
"value": "est",
"title": "tags"
},
{
"sort_index": 2,
"value": "Rhoda",
"title": "name"
},
{
"sort_index": 3,
"value": "Oneil",
"title": "lastName"
},
{
"sort_index": 4,
"value": "A5829786d114cb16e52a8959b",
"title": "passport"
},
{
"sort_index": 5,
"value": "373 Kensington Walk, Independence",
"title": "adress"
}
],
[
{
"sort_index": 0,
"value": 0,
"title": "index"
},
{
"sort_index": 1,
"value": "est",
"title": "tags"
},
{
"sort_index": 2,
"value": "Rhoda",
"title": "name"
},
{
"sort_index": 3,
"value": "Oneil",
"title": "lastName"
},
{
"sort_index": 4,
"value": "A5829786d114cb16e52a8959b",
"title": "passport"
},
{
"sort_index": 5,
"value": "373 Kensington Walk, Independence",
"title": "adress"
}
]
]
[
[
{
"sort_index": 0,
"value": 0,
"title": "index"
},
{
"sort_index": 1,
"value": "asdasdest",
"title": "tags"
},
{
"sort_index": 2,
"value": "sdfgdfRhoda",
"title": "name"
},
{
"sort_index": 3,
"value": "dfgaOneil",
"title": "lastName"
},
{
"sort_index": 4,
"value": "sdfsfsd",
"title": "passport"
},
{
"sort_index": 5,
"value": "sdfsdf373 Kensington Walk, Independence",
"title": "adress"
}
],
[
{
"sort_index": 0,
"value": 0,
"title": "index"
},
{
"sort_index": 1,
"value": "est",
"title": "tags"
},
{
"sort_index": 2,
"value": "Rhoda",
"title": "name"
},
{
"sort_index": 3,
"value": "Oneil",
"title": "lastName"
},
{
"sort_index": 4,
"value": "A5829786d114cb16e52a8959b",
"title": "passport"
},
{
"sort_index": 5,
"value": "373 Kensington Walk, Independence",
"title": "adress"
}
],
[
{
"sort_index": 0,
"value": 0,
"title": "index"
},
{
"sort_index": 1,
"value": "est",
"title": "tags"
},
{
"sort_index": 2,
"value": "Rhoda",
"title": "name"
},
{
"sort_index": 3,
"value": "Oneil",
"title": "lastName"
},
{
"sort_index": 4,
"value": "A5829786d114cb16e52a8959b",
"title": "passport"
},
{
"sort_index": 5,
"value": "373 Kensington Walk, Independence",
"title": "adress"
}
],
[
{
"sort_index": 0,
"value": 0,
"title": "index"
},
{
"sort_index": 1,
"value": "est",
"title": "tags"
},
{
"sort_index": 2,
"value": "Rhoda",
"title": "name"
},
{
"sort_index": 3,
"value": "Oneil",
"title": "lastName"
},
{
"sort_index": 4,
"value": "A5829786d114cb16e52a8959b",
"title": "passport"
},
{
"sort_index": 5,
"value": "373 Kensington Walk, Independence",
"title": "adress"
}
],
[
{
"sort_index": 0,
"value": 0,
"title": "index"
},
{
"sort_index": 1,
"value": "est",
"title": "tags"
},
{
"sort_index": 2,
"value": "Rhoda",
"title": "name"
},
{
"sort_index": 3,
"value": "Oneil",
"title": "lastName"
},
{
"sort_index": 4,
"value": "A5829786d114cb16e52a8959b",
"title": "passport"
},
{
"sort_index": 5,
"value": "373 Kensington Walk, Independence",
"title": "adress"
}
],
[
{
"sort_index": 0,
"value": 0,
"title": "index"
},
{
"sort_index": 1,
"value": "ezxczxcst",
"title": "tags"
},
{
"sort_index": 2,
"value": "Rhozxczxcda",
"title": "name"
},
{
"sort_index": 3,
"value": "Onezxcczxil",
"title": "lastName"
},
{
"sort_index": 4,
"value": "A582978zxczxc6d114cb16e52a8959b",
"title": "passport"
},
{
"sort_index": 5,
"value": "373 Kensizxcczx ngton Walk, Independence",
"title": "adress"
}
],
[
{
"sort_index": 0,
"value": 0,
"title": "index"
},
{
"sort_index": 1,
"value": "qweqwe",
"title": "tags"
},
{
"sort_index": 2,
"value": "dfgd",
"title": "name"
},
{
"sort_index": 3,
"value": "dfgdfg",
"title": "lastName"
},
{
"sort_index": 4,
"value": "A58297dfg86d114cb16e52a8959b",
"title": "passport"
},
{
"sort_index": 5,
"value": "373 Kensington Walk, Independence",
"title": "adress"
}
],
[
{
"sort_index": 0,
"value": 0,
"title": "index"
},
{
"sort_index": 1,
"value": "sdffqweest",
"title": "tags"
},
{
"sort_index": 2,
"value": "Rqwehoda",
"title": "name"
},
{
"sort_index": 3,
"value": "qweOneil",
"title": "lastName"
},
{
"sort_index": 4,
"value": "qwe",
"title": "passport"
},
{
"sort_index": 5,
"value": "373 qwe Walk, Independence",
"title": "adress"
}
],
[
{
"sort_index": 0,
"value": 0,
"title": "index"
},
{
"sort_index": 1,
"value": "vbbest",
"title": "tags"
},
{
"sort_index": 2,
"value": "zx Rhoda",
"title": "name"
},
{
"sort_index": 3,
"value": "Oneil",
"title": "lastName"
},
{
"sort_index": 4,
"value": "A582zxc9786d114cb16e52a8959b",
"title": "passport"
},
{
"sort_index": 5,
"value": "373 Kenzxczxcsington Walk, Independence",
"title": "adress"
}
],
[
{
"sort_index": 0,
"value": 0,
"title": "index"
},
{
"sort_index": 1,
"value": "zxc zxcest",
"title": "tags"
},
{
"sort_index": 2,
"value": "zxczxcRhoda",
"title": "name"
},
{
"sort_index": 3,
"value": "Ozxczxcneil",
"title": "lastName"
},
{
"sort_index": 4,
"value": "zxczxcA5829786d114cb16e52a8959b",
"title": "passport"
},
{
"sort_index": 5,
"value": "3cvb73 Kensington Walk, Independence",
"title": "adress"
}
]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment