Wpis z mikrobloga

via Wykop Mobilny (Android)
  • 0
@massejferguson: When the sort() function compares two values, it sends the values to the compare function, and sorts the values according to the returned (negative, zero, positive) value.

If the result is negative, a is sorted before b.

If the result is positive, b is sorted before a.

If the result is 0, no changes are done with the sort order of the two values.
@massejferguson: The Compare Function
The purpose of the compare function is to define an alternative sort order.

The compare function should return a negative, zero, or positive value, depending on the arguments:

function(a, b){return a - b}
When the sort() function compares two values, it sends the values to the compare function, and sorts the values according to the returned (negative, zero, positive) value.

If the result is negative, a is
via Wykop Mobilny (Android)
  • 0
@massejferguson: funkcja sort przyjmuje jako argument tzw. komparator. To jest właśnie ta funkcja a,b. Porównuje ona kolejne pary z tablicy i ustala kolejność na podstawie wyniku działania a- b. Gdy wynik dodatni to kolejność będzie b,a. Gdy ujemny to a, b. Gdy a-b==0 to nie zmienia kolejności.