0

I am wondering if I can sort this Array like this:

[ 0 ,3, 1, 4, 0 ] =>   [ 1, 3, 4, 0, 0] 

detailed rules:

  • the array contains 0 and positive numbers.
  • all the 0 are in the end.
  • all the positive numbers are sorted in ascending order.

At the beginning , I used the js's raw sort function, like:

[0, 3, 1, 4, 0].sort(function(previous_value, former_value){
  if(previous_value == 0 && former_value != 0 ) {
      return 1;
    }else if (former_value == 0 && former_value != 0) {
      return -1;
    }else if (former_value == 0 && former_value == 0) {
      return 0;
    }else{
      return previous_value - former_value;
  }
})

However, I failed. I am wondering if is it possible to implement "my sorting algorithm" using the sort function? e.g.:

[0, 3, 1, 4, 0].sort(function(previous_value, former_value){
    //code goes here
})

And it will get the correct answer:

[1, 3, 4, 0, 0]
5
  • Do you need it using ONLY sort function, or can I implement it in any other way? Jun 24, 2016 at 6:17
  • I just tested your function and seems to work as expected: jsfiddle.net/mrlew/j5qbcgch
    – mrlew
    Jun 24, 2016 at 6:24
  • Your code snippet is confusing and incorrect. The variables are named "previous" and "former" (almost synonyms, confusing), and you are only checking the "former" one in the two else if branches where you should have used both. Jun 24, 2016 at 6:25
  • @user4815162342 yes! you are right! the "previous" and "former" doesn't look good. I just don't want to name them as "a", "b" or "x/y". I will check it out!
    – Siwei
    Jun 24, 2016 at 6:33
  • There is a reason for the traditional names - you cannot assign meaning to these elements (since the sorting algorithm gives you completely arbitrary array members), so the names convey that. In a sense, they really are just some a/b/x/y which you need to compare, no more to it than that. Jun 24, 2016 at 7:23

4 Answers 4

3

Give 0 to higher priority

console.log(
  [0, 3, 1, 4, 0].sort(function(a, b) {
    return a === b ? 0 : (a === 0 ? 1 : (b === 0 ? -1 : a - b));
  })
);

With expanded if condition

console.log(
  [0, 3, 1, 4, 0].sort(function(a, b) {
    if (a === b)
      return 0;
    if (a === 0)
      return 1;
    if (b === 0)
      return -1;
    return a - b;
  })
);

2

All you need is to force your comparator to treat 0 as the highest number.

function compareNumbers(a, b) {
  if (a === 0) {
   return 1;
  }
  if (b === 0) {
   return -1;
  }
  return a - b;
}

And now you just do [0, 3, 1, 4, 0].sort(compareNumbers).

Only because it is tagged with tag, I would say that if you expect your array to be large and have high number of zeros, it might make sense to create a smaller array of not zero elements, sort it and append it with zeros to the original length.

4
  • Thanks a lot! your answer is also correct, however I could choose only answer. good job!
    – Siwei
    Jun 24, 2016 at 6:31
  • this may increase number of steps in sort() :) , jsfiddle.net/9rxnddbw Jun 24, 2016 at 6:31
  • Siwei, not a problem. He was faster :-). @PranavCBalan my point was not to be as concise as possible, but to explain what exactly I am doing with a comparator and why do I do this. Jun 24, 2016 at 6:33
  • 1
    @SalvadorDali in case both values are 0 it returns 1, just that increase the number of steps, it can be fixed by if (a !== b && a === 0) or if (b !== 0 && a === 0) Jun 24, 2016 at 6:34
1

This would be my solution with logical short circuits

var a = [0, 3, 1, 4, 0].sort((a,b) => !a && 1 || !b && -1 || a-b);
console.log(a);

1

While you have already a solution, you could use the logical OR operator for a falsy value (like 0) and give it a default value (like Infinity).

This moves zero to the end, because the default value is bigger than all other values.

var array = [ 0 ,3, 1, 4, 0 ];

array.sort(function (a,b) {
    return (a || Infinity) - (b || Infinity);
});

console.log(array);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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