Analysis of usage examples of sort() in javascript_javascript skills
This article analyzes the usage of sort() in javascript with examples. Share it with everyone for your reference. The specific analysis is as follows:
Syntax of function:
arrayObject.sort(sortby)
you think this is not the right way but you love it
The split function is also used here, the purpose is to get an array of strings, which is more commonly used. Then the values in the array are sorted through the array's sorting function sort() to obtain a new array, and then the sorted string is obtained by looping through the contents of the array.
In the example, by default, it will be sorted according to ascii code.
What if it were numbers? Give it a try~
Modify the value in p as follows:
20 38 19 32 654 2 123 454 4
The running result is: 123 19 2 20 32 38 4 454 654
It is sorted according to the character encoding, not the size of the value.
If you want to sort numbers, you need to write a few more lines of code:
The modified code is as follows:
originarr = originarr.sort(function(a,b){ return a - b; });
Run results: 2 4 19 20 32 38 123 454 654
The above sorting is in positive order. If it is in reverse order, then you need to change it again:
Just change return a - b; in the function to return b - a.
If it is alphabetical sorting, the changed code is as follows:
originarr = originarr.sort(function(a,b){ if(a > b) return -1; if(a < b) return 1; return 0; });
I hope this article will be helpful to everyone’s JavaScript programming design.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Classification and Usage Analysis of JSP Comments JSP comments are divided into two types: single-line comments: ending with, only a single line of code can be commented. Multi-line comments: starting with /* and ending with */, you can comment multiple lines of code. Single-line comment example Multi-line comment example/**This is a multi-line comment*Can comment on multiple lines of code*/Usage of JSP comments JSP comments can be used to comment JSP code to make it easier to read

How to use the exit function in C language requires specific code examples. In C language, we often need to terminate the execution of the program early in the program, or exit the program under certain conditions. C language provides the exit() function to implement this function. This article will introduce the usage of exit() function and provide corresponding code examples. The exit() function is a standard library function in C language and is included in the header file. Its function is to terminate the execution of the program, and can take an integer

WPS is a commonly used office software suite, and the WPS table function is widely used for data processing and calculations. In the WPS table, there is a very useful function, the DATEDIF function, which is used to calculate the time difference between two dates. The DATEDIF function is the abbreviation of the English word DateDifference. Its syntax is as follows: DATEDIF(start_date,end_date,unit) where start_date represents the starting date.

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

The ISNULL() function in MySQL is a function used to determine whether a specified expression or column is NULL. It returns a Boolean value, 1 if the expression is NULL, 0 otherwise. The ISNULL() function can be used in the SELECT statement or for conditional judgment in the WHERE clause. 1. The basic syntax of the ISNULL() function: ISNULL(expression) where expression is the expression to determine whether it is NULL or

How to use Apple shortcut commands With the continuous development of technology, mobile phones have become an indispensable part of people's lives. Among many mobile phone brands, Apple mobile phones have always been loved by users for their stable systems and powerful functions. Among them, the Apple shortcut command function makes users’ mobile phone experience more convenient and efficient. Apple Shortcuts is a feature launched by Apple for iOS12 and later versions. It helps users simplify their mobile phone operations by creating and executing custom commands to achieve more efficient work and

Commonly used shortcut keys in Windows 10 can save us a lot of time. Today I will introduce to you some commonly used shortcut keys, which are very convenient and fast. Let’s take a look at the specific usage methods. Win10 shortcut key usage introduction Copy, paste and other general keyboard shortcuts Press this key to perform this action Ctrl+X Cut the selected item Ctrl+C (or Ctrl+Insert) Copy the selected item Ctrl+V (or Shift+Insert) Paste the selected item Ctrl+Z Undo the operation Alt+Tab Switch between open applications Alt+F4 Close the active item, or exit the active application Windows logo key + L Lock the computer Windows logo key + D Show and hide the desktop F

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service
