Get the App
SLTechnology News&Howtos  ›  Development  › 

How js compares functions

Shulou Source: shulou.com Published: 2022-06-01 03:22:15 10月24日 Update

This article mainly shows you "js how to compare functions", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "js how to compare functions" this article.

Ratio function

The purpose of the comparison function is to define another sort order.

The comparison function should return a negative, zero, or positive value, depending on the parameter:

Function (a, b) {return Amurb}

When the sort () function compares two values, it sends the values to the comparison function and sorts them based on the values returned (negative, zero, or positive).

Example:

When comparing 40 and 100, the sort () method calls the comparison function function (40100).

This function calculates 40-100 and returns-60 (negative).

The sort function will sort 40 to a value lower than 100.

You can use the following code snippet to test numeric and alphabetical ordering:

Sort in numerical order

Var points = [40,100,1,5,25,10]; document.getElementById ("demo") [xss_clean] = points;function myFunction1 () {points.sort (); document.getElementById ("demo") [xss_clean] = points;} function myFunction2 () {points.sort (function (a, b) {return a-b}); document.getElementById ("demo") [xss_clean] = points } these are all the contents of the article "how to compare functions in js". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Functions sorting content articles order letters learning help two code parameters depending on examples values numbers methods easy to understand more organization ratio Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Linux Shulou Information Redmi MariaDB