- 浏览: 770063 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (256)
- ssh (18)
- webservice (8)
- java基础 (38)
- j2EE方方面面 (17)
- 随意涂鸭!呵呵 (2)
- 数据库 (22)
- work (10)
- XML与XML解析 (9)
- 测试 (2)
- sso (1)
- ldap (6)
- java 模板技术 (4)
- 版本管理 (1)
- 每日小点滴 (26)
- javascript (26)
- Jakarta Commons (2)
- css (6)
- 设计 (3)
- Eclipse插件开发 (3)
- BAP (3)
- web控件 (2)
- java加密解密 (4)
- 调优 (6)
- 界面技术 (3)
- java多线程 (6)
- 互联网 (2)
- 日志管理 (4)
- java调度 (3)
- rest (0)
- Python (2)
- mobile (2)
- 2016的故事 (4)
- Docker (1)
- NOSQL_Hadoop (0)
最新评论
-
promiseloney:
这个女程序员厉害了。。。
JVM调优:GC 参数 -
zxjlwt:
可以通过WebService上传一个文件吗?素人派http:/ ...
webservice传送XML大小估算 -
liaoshaoyang:
写的不错嘛 可以做参考
权限管理设计一 -
aaaaaaaaabaas:
谢谢,对我有帮助
Apache Commons Configuration使用入门 -
Jack_Wilshere:
com.smartdot.pdm.business.corp. ...
java导出txt
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
发表评论
-
Dojo query 库
2016-11-01 15:42 289Dojo Query 库的核心是一个 dojo.query ... -
javascript创建对象的几种方式 .
2013-09-02 22:30 1180// 1. 工厂方法:能创建 ... -
javascript对象创建的五种方式
2013-09-02 22:27 895<html> <head> &l ... -
navigator对象介绍
2013-09-02 22:26 1050<html> <head> ... -
当执行打印预览window.close无效
2013-09-02 22:24 2004<html> <head> &l ... -
通过js encodeURIComponent传到服务器的乱码问题
2011-01-21 14:02 9895一、场景:最近在做一个微博项目,用过微博的人都知道,微博里有话 ... -
当session失效后,无论点击那个页面,都找到顶端页面,跳到登录页面。
2010-08-18 17:15 1281当session失效后,用户点击当前页面会跳到登录页面,如果用 ... -
javascript boolean判断
2010-06-29 11:20 1319<!DOCTYPE HTML PUBLIC " ... -
利用setTimeout方法控制JS中方法的执行顺序
2009-11-06 13:23 4924JS方面中有A和B方法,B必须在A执行完之后才能执行,怎么保证 ... -
利用冒泡排序法实现select option按中文排序组件
2009-11-06 13:14 1584/* *给当前的select元素排序, *@ param ... -
JS-中文排序
2009-10-30 14:35 1000<!DOCTYPE HTML PUBLIC " ... -
Jscript中window.setInterval和window.setTimeout的区别
2009-10-23 09:40 1015一、setTimeout setTimeout(表达式,延时 ... -
window.showModalDialog介绍
2009-10-21 16:12 4540window.showModalDialog的用法 基本介绍 ... -
改变select元素原来的事件属性,并加快捷键
2009-03-04 13:57 1729<select name="commonNat ... -
javascript 之---提交到一个新打开的页面
2009-02-23 15:00 1298window.open("",&qu ... -
javascript之--Function 与function
2009-02-23 10:45 1186Assigning a function to a varia ... -
Array 之 join() input()
2009-02-18 11:26 1035<html> <head> &l ... -
js页面排序-----基础篇
2009-01-06 09:46 1913由于客户查询出来 ... -
Event/window.Event属性和方法
2009-01-04 10:57 3247一、event说明:event代表事件的状态,例如触发eve ... -
在javascript中如何屏幕HTML元素原有的事件,根据需要建立自己需要的事件
2008-12-30 14:06 1395<!DOCTYPE HTML PUBLIC " ...
相关推荐
c#中的Collections Data Structure用法array sort。是一些源码,txt格式的。
js中array的sort()方法使用介绍.docx
数组sort排序 sort比较次数,sort用法,sort常用 描述 方法sort()将在原数组上对数组元素进行排序,即排序时不创建新的数组副本。如果调用方法sort()时没有使用参数,将按字母顺序(更为精确地说,是按照字符编码的...
利用 Array.Sort()函数对数组排序,c#写的简单控制台应用程序
bp-array-sort 是错误的 Array.prototype.sort 方法浏览器实现的故障转移实现 安装 我们使用将其加载到浏览器中,但您应该能够将其与其他浏览器模块管理器一起使用。 在nodejs使用: npm i --save bp-array-sort...
JavaScript中的Array对象有自己的排序方法sort(),对数组中的数据项进行排序,但是有时候排序结果不尽如人意,比如 var arr = [12, 1, 2, 21, 3]; arr.sort(); alert(arr); 得到的结果为 1,12,2,21,3 这是...
JavaScript的Array对象有一个sort方法,用于实现对数组元素的排序,该方法默认按照数组项ASCII 字符顺序升序排列。 如[6,7,9,1,-1].sort();执行后数组变为[-1,1,6,7,9]。 对于需要降序排列或非字符串排序,该...
或许你一直在用javascript中的array的sort. 或许你一直相信它会给你正确的结果。 至少我曾经也是这样认为的,直到有一天,我看到了如下的代码 : 代码如下: [5,10,1].sort(); 或许结果有点出人意料。结果如下: 代码...
对2维数组中,指定的列进行排序。 可以对数值、字符进行排序。可以控制正序或倒序排序。 Labview v2010
javascript 中 Array.sort()方法是用来对数组项进行排序的 ,默认情况下是进行升序排列,实例代码如下: var arrA = [6,2,4,3,5,1]; arrA.sort(); [removed]ln(arrA); //结果是:1,2,3,4,5,6 sort() 方法可以接受...
深入聊聊Array的sort方法的使用技巧.详细点评protype.js中的sortBy方法
因为javascript array 之sort 经过脚本引擎优化,性能往往比自己写的好。
quick sort using array of pointer
java代码-array 的 sort fill equals 用法
Array-Sort:大会总参谋长
本篇文章主要介绍了JavaScript中数组Array.sort()的排序方法。具有很好的参考价值,下面跟着小编一起来看下吧