JS use sort method: What the hell does the example mean? America explain

Hiya!

I’m currently doing the JS Algorithms and Data Structures course and loving it. I’m at Functional Programming. There is one chapter on the sort method and I cannot for the life of me figure out what the callback function in this example means/ how it works.

Could someone explain it to me ??

function reverseAlpha(arr) { return arr.sort(function(a, b) { return a === b ? 0 : a < b ? 1 : -1; }); } reverseAlpha(['l', 'h', 'z', 'b', 's']); 

This would return the value [‘z’, ‘s’, ‘l’, ‘h’, ‘b’].

How does the callback function sort the letters in the array in descending order? Why are there division signs in there? I am so confused.

Hello @chasingthemoon

Welcome to the forum!

Check out theMDN docs on how Array.prototype.sort() works, it has detailed information in it.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.