0

I have an array [1, 85, -1, -1, 25, 0]

I need it sorted like this: [0, 1, 25, 85, -1, -1]

Tried to use the sort() method but having no luck as its not in ASC order...

Logic: The values represent days from last order. -1 represents no order. Require it ordered by most recent ordered.

9
  • 2
    what about -2? Commented Sep 10, 2020 at 11:50
  • "Tried to use the sort() method" What did that look like? Your best bet here is to do your research, search for related topics on SO, and give it a go. If you get stuck and can't get unstuck after doing more research and searching, post a minimal reproducible example of your attempt and say specifically where you're stuck. People will be glad to help. Commented Sep 10, 2020 at 11:50
  • So show us what kind of callback function you use to sort elements. What rule should be applied here? Commented Sep 10, 2020 at 11:50
  • how -1 will be greater than others? will u mean that minus values should be on last? Commented Sep 10, 2020 at 11:50
  • Separately: What's the logic? Negatives at the end? In what order? Commented Sep 10, 2020 at 11:50

3 Answers 3

3

In the sort callback, you receive two arguments (I usually call them a and b). You return a negative number if a should come before b, 0 if it doesn't matter (they're the same for sorting purposes), or a positive number if a should go after b.

In your case, since -1 goes at the end (you've said there are no other negative numbers), you just need to special-case it:

array.sort((a, b) => {
    if (a === -1) { // < 0 would also work, since there aren't any others
        return 1;
    }
    if (b === -1) { // "
        return -1;
    }
    return a- b;
});

Live Example:

const array = [1, 85, -1, -1, 25, 0];
array.sort((a, b) => {
    if (a === -1) {
        return 1;
    }
    if (b === -1) {
        return -1;
    }
    return a- b;
});
console.log(array);

That can be more concise, of course, I wrote it as above primarily for maximum clarity. But for instance:

array.sort((a, b) => a === -1 ? 1 : b === -1 ? -1 : a - b);

Personally I prefer slightly more verbose than that. But... :-)

Sign up to request clarification or add additional context in comments.

1 Comment

Well written informative answer - its just that Nina was blazing fast.
2

You could check if smaller than zero and sort the rest ascending.

var array = [1, 85, -1, -1, 25, 0];

array.sort((a, b) => (a < 0) - (b < 0) || a - b);

console.log(array);

3 Comments

All due respect (and in my experience of your contributions here, a lot is due), but wow that's difficult code to read. I wouldn't have it in a codebase.
the idea is to use a symmetric approach by using all deltas for sorting (first for groups second for values). the first part uses booleans to separate groups and the second just sorts ascending.
Oh, I understand how it works. I just think it's too tricky. :-) I'm reminded of Edsger Dijkstra's famous quote: "The competent programmer is fully aware of the limited size of his own skull. He therefore approaches his task with full humility, and avoids clever tricks like the plague." But styles differ. :-)
1

You could sort() as usual and then explicitly move the -1s to the end using splice() and push().

let arr = [1, 85, -1, -1, 25, 0];
arr.sort((a, b) => a - b);
let idx = arr.findIndex(a => a != -1);
arr.push(...arr.splice(0, idx));
console.log(arr);

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.