2

I have an array:

var q = [
    {s: 'last', o: 1000},
    {s: 's2'},
    {s: 's8'},
    {s: 's6'},
    {s: 's1', o: 1},
    {s: 's7'},
    {s: 's9'},
    {s: 's3', o: 3},
    {s: 's5', o: 5},
    {s: 's4', o: 4},
    {s: 's10', o: 10}
]

I want to sort it by field q.o, but this field can be undefined. I need to get result like the example below:

var out = [
    {s: 's1', o: 1},
    {s: 's2'},
    {s: 's3', o: 3},
    {s: 's4', o: 4},
    {s: 's5', o: 5},    
    {s: 's8'},
    {s: 's6'},
    {s: 's7'},
    {s: 's9'},
    {s: 's10', o: 10},
    {s: 'last', o: 1000}
]

upd: s is an object in real project, it contains scripts to launch. I need to launch all scripts in order as they defined, but before this I need to inject scripts which has o field to its places.

13
  • you need to filter it first on property o then, if undefined, on property s?
    – BeNdErR
    Sep 20, 2013 at 13:27
  • No, property s is an array or object. I need to sort if first by index, and then by o field.
    – rndm
    Sep 20, 2013 at 13:28
  • 1
    in your example, out[1] has no property o and it's placed before out[2]. How do you define that o: undefined comes before o: 3?
    – BeNdErR
    Sep 20, 2013 at 13:31
  • what do u mean sort by index? Sep 20, 2013 at 13:32
  • look you are already sorting it through S ,s1,o:1||s3,o:3,||s4,o:4 Sep 20, 2013 at 13:33

3 Answers 3

1

How about this?

var out = [];

var a1 = [],
    a2 = [];
for (var i = 0; i < q.length; i++) {
    if (q[i].o) {
        a1.push(q[i]);
    } else {
        a2.push(q[i]);
    }
}
a1.sort(function (a, b) {
    return a.o - b.o;
});

var aux = a1[0].o;
while (a1.length || a2.length) {
    if ((a1.length && a1[0].o == aux) || a2.length == 0) {
        if(a1[1] && a1[1].o > a1[0].o) aux++;
        out.push(a1.shift());
    } else if(a2.length) {
        out.push(a2.shift());
        aux++;
    }
}

Working demo

5
  • @rndm so how will you know how to order the ones that does not have o property? May they go to the end? If not there should be another order condition Sep 20, 2013 at 14:09
  • Just imagine that we removed all object with o field, and got array like this: var q = [ {s: 's2'}, {s: 's8'}, {s: 's6'}, {s: 's7'}, {s: 's9'} ] after that we put rest of array to places that defined in o. Sorry, I can't explain well what exactly I need because of my bad English
    – rndm
    Sep 20, 2013 at 14:14
  • @rndm I changed my answer and added another DEMO. See if it is this what you need. Sep 20, 2013 at 15:57
  • Thank you very much. It works. I wrote similar code today and now think how to opimize it
    – rndm
    Sep 20, 2013 at 20:12
  • oops, sorry, I'll fix it
    – rndm
    Sep 20, 2013 at 23:55
0

Use underscore.js and use the sortBy functionality var out = sortBy( q, 's' );

-1
var out = q.sort(function (a,b){
    if (a.o!=null && b.o!=null){
        return a.o-b.o;  
    }
    return a.s>b.s?1:-1;
});
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.