I have an array having many objects. I am trying to sort the first half of the array with ascending order. And the second half of the array with ascending order too. The code below is an array example and my way to do it. I am thinking is there a smarter way to sharpen the code and get the same result? Can anyone help? Thanks in advance!
var data = [
{id:1, x: 33},
{id:2, x: 22},
{id:3, x: 11},
{id:4, x: 3},
{id:5, x: 2},
{id:6, x: 1}
];
var data1 = [];
for(var i=0; i<3; i++){
data1.push(data[i]);
}
data1.sort (function(a,b) { return a.x - b.x; });
var data2 = [];
for(var i=3; i<6; i++){
data2.push(data[i]);
}
data2.sort (function(a,b) { return a.x - b.x; });
data = data1.concat(data2);
console.log(data);
forloops, and doing this instead:var data1 = data.splice(data.length/2); var data2 = data