While doing an online course I came across the following code. I am unable to understand the logic of this method. I will be grateful if someone can help me understand the code:
var v=[30,2,1,9,15,10,55,20,45,30,25,40,50,35];
var msg = document.getElementById("message")
msg.innerHTML +="<p><strong>Original Order:</strong>[" + v + "]<p>";
v.sort();
msg.innerHTML +="<p><strong>After Sorting Order(string):</strong>[" + v + "]<p>";
v.sort(function(a,b){return a-b;});
msg.innerHTML +="<p><strong>After Sorting Order(Ascending):</strong>[" + v + "]<p>";
v.sort(function(a,b){return b-a;});
msg.innerHTML +="<p><strong>After Sorting Order(Descending):</strong>[" + v + "]<p>";
I need to understand function(a,b){return a-b}
- how is this function helping JavaScript sorting? Please note I am new to programming.