Javascript 数组排序详解
作者:bea
如果你接触javascript有一段时间了,你肯定知道数组排序函数sort,sort是array原型中的一个方法,即array.prototype.sort(),sort(compareFunction),其中compareFunction是一个比较函数,下面我们看看来自Mozilla MDN 的一段描述: If compareFunction is not supplied, elements are sorted by converting them to strings
如果你接触javascript有一段时间了,你肯定知道数组排序函数sort,sort是array原型中的一个方法,即array.prototype.sort(),sort(compareFunction),其中compareFunction是一个比较函数,下面我们看看来自Mozilla MDN 的一段描述: 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.
下面看些简单的例子:
代码如下:
// Output [1, 2, 3]
console.log([3, 2, 1].sort());
// Output ["a", "b", "c"] console.log(["c", "b", "a"].sort());
// Output [1, 2, "a", "b"] console.log(["b", 2, "a", 1].sort());
从上例可以看出,默认是按字典中字母的顺序来排序的。
幸运的是,sort接受一个自定义的比较函数,如下例:
代码如下:
function compareFunction(a, b) {
if( a > b) {
return -1;
}else if(a < b) {
return 1;
}else {
return 0;
}
}
//Outputs ["zuojj", "Benjamin", "1"]
console.log(["Benjamin", "1", "zuojj"].sort(compareFunction));
排序完我们又有个疑问,如何控制升序和降序呢?
代码如下:
function compareFunction(flag) {
flag = flag ? flag : "asc";
return function(a, b) {
if( a > b) {
return flag === "desc" ? -1 : 1;
}else if(a < b) {
return flag === "desc" ? 1 : -1;
}else {
return 0;
}
};
}
//Outputs ["1", "Benjamin", "zuojj"]
console.log(["Benjamin", "1", "zuojj"].sort(compareFunction()));
//Outputs ["zuojj", "Benjamin", "1"]
console.log(["Benjamin", "1", "zuojj"].sort(compareFunction("desc")));
comparFunction的排序规则是这样的: 1.If it returns a negative number, a will be sorted to a lower index in the array. 2.If it returns a positive number, a will be sorted to a higher index. 3.And if it returns 0 no sorting is necessary.
下面我们来看看摘自Mozilla MDN上的一段话: The behavior of the sort method changed between JavaScript 1.1 and JavaScript 1.2.为了解释这段描述,我们来看个例子:
In JavaScript 1.1, on some platforms, the sort method does not work. This method works on all platforms for JavaScript 1.2.
In JavaScript 1.2, this method no longer converts undefined elements to null; instead it sorts them to the high end of the array.详情请戳这里。
代码如下:
var arr = [];
arr[0] = "Ant";
arr[5] = "Zebra";
//Outputs ["Ant", 5: "Zebra"]
console.log(arr);
//Outputs 6
console.log(arr.length);
//Outputs "Ant*****Zebra"
console.log(arr.join("*"));
//排序
var sortArr = arr.sort();
//Outputs ["Ant", "Zebra"]
console.log(sortArr);
//Outputs 6
console.log(sortArr.length);
//Outputs "Ant*Zebra****"
console.log(sortArr.join("*"));
希望本文对你学习和了解sort()方法有帮助,文中不妥之处还望批评斧正。
参考链接:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
有用 | 无用
下面看些简单的例子:
代码如下:
// Output [1, 2, 3]
console.log([3, 2, 1].sort());
// Output ["a", "b", "c"] console.log(["c", "b", "a"].sort());
// Output [1, 2, "a", "b"] console.log(["b", 2, "a", 1].sort());
从上例可以看出,默认是按字典中字母的顺序来排序的。
幸运的是,sort接受一个自定义的比较函数,如下例:
代码如下:
function compareFunction(a, b) {
if( a > b) {
return -1;
}else if(a < b) {
return 1;
}else {
return 0;
}
}
//Outputs ["zuojj", "Benjamin", "1"]
console.log(["Benjamin", "1", "zuojj"].sort(compareFunction));
排序完我们又有个疑问,如何控制升序和降序呢?
代码如下:
function compareFunction(flag) {
flag = flag ? flag : "asc";
return function(a, b) {
if( a > b) {
return flag === "desc" ? -1 : 1;
}else if(a < b) {
return flag === "desc" ? 1 : -1;
}else {
return 0;
}
};
}
//Outputs ["1", "Benjamin", "zuojj"]
console.log(["Benjamin", "1", "zuojj"].sort(compareFunction()));
//Outputs ["zuojj", "Benjamin", "1"]
console.log(["Benjamin", "1", "zuojj"].sort(compareFunction("desc")));
comparFunction的排序规则是这样的: 1.If it returns a negative number, a will be sorted to a lower index in the array. 2.If it returns a positive number, a will be sorted to a higher index. 3.And if it returns 0 no sorting is necessary.
下面我们来看看摘自Mozilla MDN上的一段话: The behavior of the sort method changed between JavaScript 1.1 and JavaScript 1.2.为了解释这段描述,我们来看个例子:
In JavaScript 1.1, on some platforms, the sort method does not work. This method works on all platforms for JavaScript 1.2.
In JavaScript 1.2, this method no longer converts undefined elements to null; instead it sorts them to the high end of the array.详情请戳这里。
代码如下:
var arr = [];
arr[0] = "Ant";
arr[5] = "Zebra";
//Outputs ["Ant", 5: "Zebra"]
console.log(arr);
//Outputs 6
console.log(arr.length);
//Outputs "Ant*****Zebra"
console.log(arr.join("*"));
//排序
var sortArr = arr.sort();
//Outputs ["Ant", "Zebra"]
console.log(sortArr);
//Outputs 6
console.log(sortArr.length);
//Outputs "Ant*Zebra****"
console.log(sortArr.join("*"));
希望本文对你学习和了解sort()方法有帮助,文中不妥之处还望批评斧正。
参考链接:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- JavaScript极简入门教程(一):基础篇
- 上传图片预览JS脚本 Input file图片预览的实现示例
- 实用框架(iframe)操作代码
- form.submit()不能提交表单的原因分析
- Google Maps API地图应用示例分享
- 深入分析JQuery和JavaScript的异同
- jquery实现导航固定顶部的效果仿蘑菇街
- 用jquery模仿的a的title属性的例子
- 运用jQuery定时器的原理实现banner图片切换
- 用简洁的jQuery方法toggleClass实现隔行换色
- jQuery实现的导航条切换可显示隐藏
- 原生js和jQuery随意改变div属性style的名称和值
- 一个不错的js html页面倒计时可精确到秒
- js实现select跳转功能代码
- Javascript原型链和原型的一个误区
- Javascript this 关键字 详解
- Javascript 构造函数详解
- Javascript中Array.prototype.map()详解
- javascript数组详解