Home Web Front-end JS Tutorial Introduction to the use of javascript array sorting functions sort and reverse_javascript skills

Introduction to the use of javascript array sorting functions sort and reverse_javascript skills

May 16, 2016 pm 05:13 PM
sort Array sort

First, let’s talk about the reverse method.

The reverse method reverses the position of elements in an Array object. During execution, this method does not create a new Array object.

For example:

Copy code The code is as follows:

var array1 = [ 'a','cc','bb','hello',false,0,3];
var array2 = [3,5,2,1,7,9,10,13];
array1.reverse();
array2.reverse();
alert(array1);
alert(array2);

If the array only contains numbers, then the numbers will Sort in descending order. If the array contains other types, reverse the array and return the array.

sort method

Returns an Array object whose elements have been sorted.
Copy code The code is as follows:

arrayobj.sort(sortfunction)

Parameters

arrayObj

Required. Any Array object.

sortFunction

Optional. is the name of the function used to determine the order of elements. If this parameter is omitted, the elements will be sorted in ascending ASCII character order. The

sort method sorts Array objects appropriately; no new Array objects are created during execution.

If a function is provided for the sortfunction argument, the function must return one of the following values:

A negative value if the first argument passed is smaller than the second argument.
Zero if both arguments are equal.
Positive value if the first parameter is larger than the second parameter.

Example 1: ()
Copy code The code is as follows:

var a, l; // Declare variables.
a = ["X" ,"y" ,"d", "Z", "v","m","r",false,0];
l = a.sort(); // Sort the array.
alert(l); // Return the sorted array.

In this example, if the comparison function is not passed in, the elements will be arranged in ascending order according to ASCII character order. In addition, this array contains multiple types of data, so even if the comparison function is passed in, it will still Arrange in ascending order according to ASCII character order.

For example
Copy code The code is as follows:

var a, l; // Declare variables.
a = ["X" ,"y" ,"d", "Z", "v","m","r",false,0];
l = a.sort(); // Sort the array.
alert(l); // Return the sorted array.
ll = a.sort(compack);
alert(ll);//The return is the same as above
function compack(a,b){
return a-b;
}

When we need to sort numbers, we can use the sort method. As long as we pass a comparison function to it, we can easily ascend and descend.

Ascending order:
Copy code The code is as follows:

var a, l ; // Declare variables.
a = [6,8,9,5.6,12,17,90];
l = a.sort(compack); // Sort array.
alert(l); // Return the sorted array.

function compack(a,b){
return a-b;
}

Descending order:
Copy the code The code is as follows:

var a, l; // Declare variables.
a = [6,8,9,5.6,12,17,90];
l = a.sort(compack); // Sort array.
alert(l); // Return the sorted array.

function compack(a,b){
return b-a;
}

In the comparison function, return a-b in ascending order and b-a in descending order.
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explore the underlying principles and algorithm selection of the C++sort function Explore the underlying principles and algorithm selection of the C++sort function Apr 02, 2024 pm 05:36 PM

The bottom layer of the C++sort function uses merge sort, its complexity is O(nlogn), and provides different sorting algorithm choices, including quick sort, heap sort and stable sort.

How to implement drag-and-drop sorting and drag-and-drop operations in uniapp How to implement drag-and-drop sorting and drag-and-drop operations in uniapp Oct 19, 2023 am 09:39 AM

Uniapp is a cross-platform development framework. Its powerful cross-end capabilities allow developers to develop various applications quickly and easily. It is also very simple to implement drag-and-drop sorting and drag-and-drop operations in Uniapp, and it can support drag-and-drop operations of a variety of components and elements. This article will introduce how to use Uniapp to implement drag-and-drop sorting and drag-and-drop operations, and provide specific code examples. The drag-and-drop sorting function is very common in many applications. For example, it can be used to implement drag-and-drop sorting of lists, drag-and-drop sorting of icons, etc. Below we list

Fast array sorting method that preserves key names in PHP Fast array sorting method that preserves key names in PHP May 02, 2024 pm 03:06 PM

Fast array sorting method in PHP that preserves key names: Use the ksort() function to sort the keys. Use the uasort() function to sort using a user-defined comparison function. Practical case: To sort an array of user IDs and scores by score while retaining the user ID, you can use the uasort() function and a custom comparison function.

JS array sorting: in-depth analysis of the working principle and mechanism of the sort() method JS array sorting: in-depth analysis of the working principle and mechanism of the sort() method Dec 28, 2023 am 11:47 AM

To deeply understand JS array sorting: the principles and mechanisms of the sort() method, specific code examples are required. Introduction: Array sorting is one of the very common operations in our daily front-end development work. The array sorting method sort() in JavaScript is one of the most commonly used array sorting methods. However, do you really understand the principles and mechanisms of the sort() method? This article will give you an in-depth understanding of the principles and mechanisms of JS array sorting, and provide specific code examples. 1. Basic usage of sort() method

Sort array using Array.Sort function in C# Sort array using Array.Sort function in C# Nov 18, 2023 am 10:37 AM

Title: Example of using the Array.Sort function to sort an array in C# Text: In C#, array is a commonly used data structure, and it is often necessary to sort the array. C# provides the Array class, which has the Sort method to conveniently sort arrays. This article will demonstrate how to use the Array.Sort function in C# to sort an array and provide specific code examples. First, we need to understand the basic usage of the Array.Sort function. Array.So

How to keep key names after sorting PHP array by value? How to keep key names after sorting PHP array by value? May 02, 2024 pm 04:09 PM

The way to sort an array by value in PHP while preserving the key names is to use the usort() function to sort the array by value. Pass an anonymous function to the usort() function as a comparison function, which returns the difference in element values. usort() will sort the array according to the anonymous function while keeping the key names unchanged.

Guide to writing a custom sorting algorithm for PHP arrays Guide to writing a custom sorting algorithm for PHP arrays Apr 27, 2024 pm 06:12 PM

How to write a custom PHP array sorting algorithm? Bubble sort: Sorts an array by comparing and exchanging adjacent elements. Selection sort: Select the smallest or largest element each time and swap it with the current position. Insertion sort: Insert elements one by one into the sorted part.

Sort arrays according to custom sorting rules in PHP, retaining original key names Sort arrays according to custom sorting rules in PHP, retaining original key names May 04, 2024 am 09:27 AM

In PHP, use the uasort() function to sort an array according to custom sorting rules while retaining the original key names. A custom comparison function is a function that takes two elements as input and returns an integer: a negative number means the former is less than the latter, zero means they are equal, and a positive number means the former is greater than the latter.

See all articles