0

I want to use a callback function for when the sort method has finished sorting the numbers inside the array. I would think that it must look something like this:

  totals.sort(function(a, b){b - a; continuation()});

Somehow though, my code isn't doing what it is supposed to do. Therefore I am wondering wheter or not it is because of this line of code. In case it is, can anyone please tell me the appropriate way to do this.

5
  • Can you provide a minimal reproducible example? Commented Jul 25, 2017 at 12:43
  • 2
    You don't need to do this, sort is synchronous. Commented Jul 25, 2017 at 12:45
  • Also, your sort function needs to return a value. Commented Jul 25, 2017 at 12:45
  • oh of course, I feel a little embarrassed for not thinking of that. Anyway, thank you very much! Commented Jul 25, 2017 at 12:47
  • Also, you should return a value in your compare function + this function is called numerous times for sorting purposes. Not once when sorting is finished. Commented Jul 25, 2017 at 12:47

1 Answer 1

0

The sort function doesn't accept an argument that does that, so you can't.

It isn't needed though: sort isn't asynchronous in the first place.

Just sort the array, then do whatever else you want to do afterwards.

totals.sort(function(a, b){b - a; });
continuation()
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.