0

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);
3
  • 2
    codereview.stackexchange.com Commented Mar 29, 2016 at 20:12
  • can you tell us how you want to order it? by id or x? Commented Mar 29, 2016 at 20:13
  • 1
    FYI, you can shorten it a bit by getting rid of the for loops, and doing this instead: var data1 = data.splice(data.length/2); var data2 = data Commented Mar 29, 2016 at 20:15

5 Answers 5

3

Using splice would tighten it up:

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

// splice out and sort the first half of data
var data1 = data.splice(0,data.length / 2);
data1.sort (function(a,b) { return a.x - b.x; });

// sort the second half
data.sort (function(a,b) { return a.x - b.x; });

data = data1.concat(data);
console.log(data);
Sign up to request clarification or add additional context in comments.

2 Comments

why this data.splice(1,data.length / 2) should work in such case ? console.log(data) gives the following order of x attribute: 3, 11, 22, 1, 2, 33
@nnnnnn yes, it should. Fixing.
2

You can slice the array with Array#slice() and concat with Array#concat().

function sortX(a, b) {
    return a.x - b.x;
}

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 }],
    first = data.slice(0, data.length / 2 | 0).sort(sortX),
    second = data.slice(data.length / 2 | 0, data.length).sort(sortX);

data = first.concat(second);
document.write('<pre>' + JSON.stringify(data, 0, 4) + '</pre>');

Comments

1
/**
 * This method allows you to split array in two peaces
 * @param Array array - the array which you want to split
 * @returns array
 */
function sort2Array(array) {
    var results = [],
        length = Math.ceil(array.length / 2),
        iterations = array.length / length;
    for (var i = 0; i < iterations; i++) {
        var peace = array.slice(i * length, (i + 1) * length);
        results.push(peace.sort(function(a, b) { return a.x - b.x; }));
    }

    return results[0].concat(results[1]);
}

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

sort2Array(data);

Comments

0
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 sort_fn = function(a,b) { return a.x - b.x; };
function half_sort(p) {
    var m = p.length/2;
    return Array.prototype.concat(
      p.slice(0, m).sort(sort_fn),
      p.slice(m).sort(sort_fn) )
}
var result = half_sort(data)

Comments

0

Using ES6

let 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}  
];
const up = data.slice(0,data.length/2).sort((prev,next) => prev.x - next.x);
const low = data.slice(data.length/2,data.length).sort((prev,next) =>  next.x - prev.x);
data = up.concat(low)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.