Func is a callback, that is, a callback function. It is needed so that the sort function "knows" how to compare the sorted objects with each other. If you have ever tried to write any sorting implementation, you should have seen that the sorting algorithms perform many comparisons of the values being erased in order to properly arrange them in the resulting sequence. In the case of numbers or strings, JS is generally able to independently understand which value is greater than / less than / is equal to another. However, in the case of complex types, no one except the programmer himself can know this. In this case, a callback comes to the rescue - the sort function uses it when comparing two values, feeding it two compared values as arguments. Depending on the returned result, sort can "understand" which of the values is greater. Let's say you have such an array that you want to sort by the Id field:
var arr = [ { Id: 3, Name : "Вася" },{ Id: 1, Name : "Петя" },{ Id: 2, Name : "Витя" } ]
and such kollbek:
function (a, b) { return a.Id - b.Id; }
and you are trying to compare the sort function with it:
arr.sort(function (a, b) { return a.Id - b.Id; });
as a result, you get an array sorted by Id:
[ { Id: 1, Name : "Петя" },{ Id: 2, Name : "Витя" }, { Id: 3, Name : "Вася" } ]
It works like this: if the return value is positive (that is, a.Id is greater than b.Id), then the sort function understands that object a is larger than object b, if the value is negative (a.Id is smaller than b.Id), then b is smaller than a and therefore must be in a higher position in the sorted sequence. If the return value is zero, then the objects are equal.
Basically, the sorting function is needed just for sorting "complex" types, in these cases JavaScript is not able to define an algorithm for comparing objects. However, no one bothers to use it for simple types. This can be useful for example if you want to sort the sequence according to some other principle.