Home > front end >  The function is passed as a parameter to the sort () method
The function is passed as a parameter to the sort () method

Time:02-04

Don't understand, is running at first sight, internal return1 return2 what is the role of

CodePudding user response:

It is not very good understand? Do you want to sort, be sure to compare, so the parameters of the function needs is used to compare two elements, namely value1, value2,
Then how to compare? This function is agreed, if value1 and value2 is equal, it returns 0; If value1 is less than value2, it returns a negative number (usually in order to facilitate use - 1, because 1 is a negative number). If value1 is greater than value2, it returns a positive number (usually in order to facilitate with 1, because 1 is positive),
Then sort method, according to the return value to determine whether the exchange of value1 and value2 position, so it can achieve the purpose of sorting,
Understand the above function, compare function could be simplified, i.e., to the following can
The function compare (value1, value2) {
Return value1 to value2;
}
If value1 and value2 equal, value1 - value2 is 0;
If value1 is less than value2, value1 - value2 is negative;
If value1 is greater than value2, value1 to value2 is positive,

CodePudding user response:

It is not very good understand? Do you want to sort, be sure to compare, so the parameters of the function needs is used to compare two elements, namely value1, value2,
Then how to compare? This function is agreed, if value1 and value2 is equal, it returns 0; If value1 is less than value2, it returns a negative number (usually in order to facilitate use - 1, because 1 is a negative number). If value1 is greater than value2, it returns a positive number (usually in order to facilitate with 1, because 1 is positive),
Then sort method, according to the return value to determine whether the exchange of value1 and value2 position, so it can achieve the purpose of sorting,
Understand the above function, compare function could be simplified, i.e., to the following can
The function compare (value1, value2) {
Return value1 to value2;
}
If value1 and value2 equal, value1 - value2 is 0;
If value1 is less than value2, value1 - value2 is negative;
If value1 is greater than value2, value1 to value2 is positive,

CodePudding user response:

reference 1st floor qybao response:
it is not very good understand? Do you want to sort, be sure to compare, so the parameters of the function needs is used to compare two elements, namely value1, value2,
Then how to compare? This function is agreed, if value1 and value2 is equal, it returns 0; If value1 is less than value2, it returns a negative number (usually in order to facilitate use - 1, because 1 is a negative number). If value1 is greater than value2, it returns a positive number (usually in order to facilitate with 1, because 1 is positive),
Then sort method, according to the return value to determine whether the exchange of value1 and value2 position, so it can achieve the purpose of sorting,
Understand the above function, compare function could be simplified, i.e., to the following can
The function compare (value1, value2) {
Return value1 to value2;
}
If value1 and value2 equal, value1 - value2 is 0;
If value1 is less than value2, value1 - value2 is negative;
If value1 is greater than value2, value1 to value2 is positive,

Before you thank you thank you, just don't understand, how can sort by the positive, negative, to sort, almost understand now
  • Related