0

I'm going over this sorting tutorial and I'm having problems understanding it. I see that the rules are the following when passing in a sortfunction to .sort().

  • Less than 0: Sort "a" to be a lower index than "b"
  • Zero: "a" and "b" should be considered equal, and no sorting performed.
  • Greater than 0: Sort "b" to be a lower index than "a".

and I'm testing this snippet:

var myarray=[25, 8, 7, 41];
console.log(myarray);
console.log(myarray.sort(function(a,b) {
    console.log("a : " , a , " b : " , b, " oper : " , b-a, " arr ");
    return b -a
}));

the main part is return b -a and this should return the array in descending order I think because if b is larger than a it will be placed in a lower index. I think I'm having a hard time under standing what exactly "lower" means. does it mean that it b goes directly before a or does b go to the beginning of the array?

here is where I'm getting confused. The result of the code is:

[25, 8, 7, 41]
a : 25  b : 8  oper : -17  arr
a : 8  b : 7  oper : -1  arr 
a : 7  b : 41  oper : 34  arr 
a : 25  b : 41  oper : 16  arr 
[41, 25, 8, 7]

here is just my thinking

since the first 2 comparisons produce a negative a goes before b so at the end of the first 2 comparisons the array should be something like [25,8,7,41] (no change).

Then the next comparison is a positive number(34) so so I think b goes before a so it will be now [25,8,41,7] then I don't why it compares 25 and 41 next How is a = 25 and b = 41? can you also show me how to view the array as it is being transformed I tried to do that without success. Thanks in advance.

If you see another question that explains this sorting breakdown in detail I'll be happy to close this question. I don't mean to ask useless questions on SO. most tuts just explain how to sort in general but not break it down. Thank you

12
  • So if I understand right, you want explanation of how sorting works. Right? Commented Mar 25, 2016 at 3:20
  • I think your issue is that you are assuming it is using some sort of bubble sort -- it is not. Commented Mar 25, 2016 at 3:21
  • 1
    What exactly is your question? The details of how .sort() does all it's comparisons is implementation specific and not part of any specification. The point is merely that it uses some algorithm to compare elements in the array and uses your callback function in order to get the result of comparing two elements. Commented Mar 25, 2016 at 3:21
  • 1
    Reverse engineering the sorting algorithm based on which comparisons are used and when would be tedious and horribly boring without teaching you much. Why not learn about the different algorithms instead? Commented Mar 25, 2016 at 3:26
  • 1
    can you tell me why we are comparing 25 and 41 last. I guess I got confused because because all the other comparisons looks like they are going in order but those numbers were not in order. I guess they were pushed to the lower indexes. Maybe I'm having a hard time visualizing it. Commented Mar 25, 2016 at 3:32

1 Answer 1

1

So is there a way to show the sort process? like when you do a for loop you can console.log the activity your looping. can you console.log the steps that it takes to sort the array? I guess not.

An approach using Math.min() , Math.max() ; Array.prototype.splice()

// `arr` : input array
function _sort(arr) { 
  // declare `min` variable
  // `res` : array that will be returned
  var min = null, res = []; console.log("step 1", min, res);
  // while `arr` contains elements
  while (arr.length) {
    // set `min` to smallest number in `arr`
    min = Math.min.apply(Math, arr); console.log("step 2", min);
    // push `min` to `res` array
    res.push(min); console.log(res);
    // remove `min` element from `arr` array
    arr.splice(arr.indexOf(min), 1); console.log("step 3", arr);
  }
  // return `res` array 
  return res
}

console.log("step 4", _sort([27, 1, 3, 14]))

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

2 Comments

so it's safe to assume that you are showing me a way to recreate a sorting function. but just to confirm this is not what JS does for .sort() right? Thanks for showing me this method.

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.