`

Array之sort()

阅读更多

Syntax
sort(compareFunction)

 

Description
If compareFunction is not supplied, elements are sorted by converting them to strings and comparing strings in lexicographic ("dictionary" or "telephone book," not numerical) order. For example, "80" comes before "9" in lexicographic order, but in a numeric sort 9 comes before 80.

If compareFunction is supplied, the array elements are sorted according to the return value of the compare function. If a and b are two elements being compared, then:

  • If compareFunction(a, b) is less than 0, sort b to a lower index than a.

  • If compareFunction(a, b) returns 0, leave a and b unchanged with respect to each other, but sorted with respect to all different elements.

  • If compareFunction(a, b) is greater than 0, sort b to a higher index than a.

So, the compare function has the following form:

function compare(a, b) {
   if (a is less than b by some ordering criterion)
      return -1
   if (a is greater than b by the ordering criterion)
      return 1
   // a must be equal to b
   return 0
}

To compare numbers instead of strings, the compare function can simply subtract b from a:

function compareNumbers(a, b) {
   return a - b
}

 

 

示例:

<html>
	<head>
 <SCRIPT>
	//此例主要用来说明Array对象的sort(compareFunction) 方法的用法
//如果省略compareFunction 对象,则按照英文字母顺序排,不管Array中的类型是数字还是字符型,sort默认的是升序
//The following example creates four arrays and displays the original array, 
//then the sorted arrays. The numeric arrays are sorted without, then with, a compare function. 

stringArray = new Array("Blue","Humpback","Beluga")
numericStringArray = new Array("80","9","700")
numberArray = new Array(40,1,5,200)
mixedNumericArray = new Array("80","9","700",40,1,5,200) 

function compareNumbers(a, b) {
   return a - b
} 

document.write("<B>stringArray:</B> " + stringArray.join() +"<BR>")
document.write("<B>Sorted:</B> " + stringArray.sort() +"<P>") 


document.write("<B>numberArray:</B> " + numberArray.join() +"<BR>")
document.write("<B>Sorted without a compare function:</B> " + numberArray.sort() +"<BR>")
document.write("<B>Sorted with compareNumbers:</B> " + numberArray.sort(compareNumbers) +"<P>") 

document.write("<B>numericStringArray:</B> " + numericStringArray.join() +"<BR>")
document.write("<B>Sorted without a compare function:</B> " + numericStringArray.sort() +"<BR>")
document.write("<B>Sorted with compareNumbers:</B> " + numericStringArray.sort(compareNumbers) +"<P>") 

document.write("<B>mixedNumericArray:</B> " + mixedNumericArray.join() +"<BR>")
document.write("<B>Sorted without a compare function:</B> " + mixedNumericArray.sort() +"<BR>")
document.write("<B>Sorted with compareNumbers:</B> " + mixedNumericArray.sort(compareNumbers) +"<BR>")	
 </SCRIPT> 
	</head>
</html>


 

 

上例运行结果:

stringArray: Blue,Humpback,Beluga
Sorted: Beluga,Blue,Humpback
numberArray: 40,1,5,200
Sorted without a compare function: 1,200,40,5
Sorted with compareNumbers: 1,5,40,200

numericStringArray: 80,9,700
Sorted without a compare function: 700,80,9
Sorted with compareNumbers: 9,80,700

mixedNumericArray: 80,9,700,40,1,5,200
Sorted without a compare function: 1,200,40,5,700,80,9
Sorted with compareNumbers: 1,5,9,40,80,200,700

 

分享到:
评论

相关推荐

    Array sort.rar

    在这个名为“Array sort”的压缩包中,可能包含了一些示例源代码,演示了如何使用`Collections`数据结构和`Array.Sort()`方法。这些源码可能是以`.txt`格式存储的,可以通过文本编辑器打开阅读。在这些代码中,...

    利用 Array.Sort()函数对数组排序

    在C#编程语言中,`Array.Sort()`方法是用于对数组进行排序的关键工具。这个方法提供了对数组元素的快速、高效排序,适用于多种数据类型,包括整型、浮点型、字符串以及自定义对象。本篇文章将深入探讨`Array.Sort()`...

    2D-array-sort.zip_LabVIEW 数组_labview_labview array_perl sort arr

    本案例中的"2D-array-sort.zip"是一个LabVIEW实验,它着重于2维数组的排序功能,允许用户对数组的特定列进行升序或降序排序。这个实验基于LabVIEW 2010版本,所以它兼容的是该版本的功能和语法。 首先,我们要理解2...

    详解数组Array.sort()排序的方法

    数组sort排序 sort比较次数,sort用法,sort常用 描述 方法sort()将在原数组上对数组元素进行排序,即排序时不创建新的数组副本。如果调用方法sort()时没有使用参数,将按字母顺序(更为精确地说,是按照字符编码的...

    js中array的sort()方法使用介绍.docx

    ### JavaScript中Array的sort()方法使用详解 #### 一、引言 在JavaScript编程中,`Array`对象是处理和操作数据集的核心工具之一。其中,`sort()`方法作为Array对象的一个重要成员方法,用于对数组中的元素进行排序...

    array-sort:bp-array-sort 是错误的 Array.prototype.sort 方法浏览器实现的故障转移实现

    bp-array-sort 是错误的 Array.prototype.sort 方法浏览器实现的故障转移实现 安装 我们使用将其加载到浏览器中,但您应该能够将其与其他浏览器模块管理器一起使用。 在nodejs使用: npm i --save bp-array-sort...

    数组Array的排序sort方法

    JavaScript中的Array对象有自己的排序方法sort(),对数组中的数据项进行排序,但是有时候排序结果不尽如人意,比如 var arr = [12, 1, 2, 21, 3]; arr.sort(); alert&#40;arr&#41;; 得到的结果为 1,12,2,21,3 这是...

    js模拟实现Array的sort方法

    JavaScript中的Array对象提供了一个强大的`sort()`方法,用于对数组元素进行排序。默认情况下,`sort()`会按照数组项的ASCII字符顺序进行升序排列。例如,数组`[6,7,9,1,-1].sort();`会被排序为`[-1,1,6,7,9]`。然而...

    js中array的sort()方法使用介绍

    或许你一直在用javascript中的array的sort. 或许你一直相信它会给你正确的结果。 至少我曾经也是这样认为的,直到有一天,我看到了如下的代码 : 代码如下: [5,10,1].sort(); 或许结果有点出人意料。结果如下: 代码...

    c语言-leetcode题解之0912-sort-an-array

    c语言入门 c语言_leetcode题解之0912_sort_an_array

    array_array_

    - 诸如两数之和、三数之和、寻找数组中的最长连续序列、旋转数组、子数组最大和等都是常见的数组题目,它们锻炼了程序员处理数组数据的能力。 在"array_array_"这个压缩包中,很可能包含了这些概念的Java代码实现...

    js数组Array sort方法使用深入分析

    javascript 中 Array.sort()方法是用来对数组项进行排序的 ,默认情况下是进行升序排列,实例代码如下: var arrA = [6,2,4,3,5,1]; arrA.sort(); [removed]ln(arrA); //结果是:1,2,3,4,5,6 sort() 方法可以接受...

    Ext.Array例子

    7. **排序**:`sort()` 方法对数组进行排序,可以自定义比较函数,如 `array.sort(function(a, b){ return a - b; })` 8. **过滤**:`filter()` 方法根据给定的条件过滤数组元素,`Ext.Array.filter(array, fn, ...

    MATLAB语言中sort函数的用法

    sortedArray = sort(array) ``` 这里的`array`是你想要排序的输入数组,可以是向量或者矩阵。`sortedArray`则是返回的已排序的新数组。默认情况下,`sort`函数按照升序(从小到大)排列数值,对于字符数组则是按...

    js 重构Array的sort排序方法

    ### JavaScript 重构 Array 的 sort() 方法 #### 一、引言 在JavaScript中,`Array.prototype.sort()` 是一个非常常用且强大的方法,用于对数组中的元素进行排序。默认情况下,`sort()` 方法按照字符串的Unicode码...

    arm_max和arm_sort两个函数的使用.docx

    arm_sort_f32(&Struct_sort, array_data, data_sort, LEN); // 其他程序逻辑... } ``` 在这个示例中,我们首先定义了一个浮点数数组`array_data`,然后在`main`函数中,我们调用`arm_max_f32`找到数组中的最大...

    C#简单排序——Sort(2005)

    本篇文章主要探讨了2005年版本的C#中几种常见的排序方法,包括`Array.Sort()`、`ListViewItem.Sort()`以及自定义排序规则的实现方式——实现`IComparer`接口。以下是对这些知识点的详细说明: 1. **Array.Sort()** ...

    深入聊聊Array的sort方法的使用技巧.详细点评protype.js中的sortBy方法

    `Array.prototype.sort`方法是JavaScript中用于对数组进行排序的核心函数。它接受一个可选的比较函数作为参数,根据这个函数的返回值来决定数组元素的顺序。在深入讲解使用技巧之前,我们先来理解其基本用法。 1. ...

Global site tag (gtag.js) - Google Analytics