# javascript 对数组 排序

Jack Wang 2010-02-03 06:25:27

...全文
106 11 打赏 收藏 举报

11 条回复

KK3K2005 2010-02-04
• 打赏
• 举报

yixianggao 2010-02-04
• 打赏
• 举报

Number升降序（2）+ Date升降序（2）+ Ascii升降序（2）+ 汉字升降序（2）！

L@_@K
``````<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<title>Array Sort By Type</title>
<meta name="generator" content="editplus" />
<meta name="author" content="" />
<meta name="keywords" content="" />
<meta name="description" content="" />

<body>

<script type="text/javascript">
<!--

Array.prototype.SortNumberOrderByAscending = function() {
return this.sort(function(ele1, ele2) {
return (ele1 - ele2);
});
};
Array.prototype.SortNumberOrderByDescending = function() {
return this.sort(function(ele1, ele2) {
return (ele2 - ele1);
});
};

Array.prototype.SortDateOrderByAscending = function() {
return this.sort(function(ele1, ele2) {
return (ele1.valueOf() - ele2.valueOf());
});
};
Array.prototype.SortDateOrderByDescending = function() {
return this.sort(function(ele1, ele2) {
return (ele2.valueOf() - ele1.valueOf());
});
};

Array.prototype.SortAsciiStringOrderByAscending = function() {
return this.sort();
};
Array.prototype.SortAsciiStringOrderByDescending = function() {
return this.sort().reverse();
};

Array.prototype.SortChineseStringOrderByAscending = function() {
return this.sort(function(ele1, ele2) {
return ele1.localeCompare(ele2);
});
};
Array.prototype.SortChineseStringOrderByDescending = function() {
return this.sort(function(ele1, ele2) {
return -ele1.localeCompare(ele2);
});
};

var numberArray = new Array(2, 101, 11, 131, 20, 1);
document.write(numberArray, "<br />");
document.write(numberArray.sort(), "<br />");
document.write(numberArray.SortNumberOrderByAscending(), "<br />");
document.write(numberArray.SortNumberOrderByDescending(), "<br />");
document.write("<hr />");

Date.prototype.toString = Date.prototype.toLocaleString;
var dateArray = new Array(new Date(2009, 1, 2), new Date(2010, 11, 1), new Date(), new Date(1999, 1, 13));
document.write(dateArray, "<br />");
document.write(dateArray.sort(), "<br />");
document.write(dateArray.SortDateOrderByAscending(), "<br />");
document.write(dateArray.SortDateOrderByDescending(), "<br />");
document.write("<hr />");

var asciiArray = new Array("zewr", "iue", "pieb", "oine");
document.write(asciiArray, "<br />");
document.write(asciiArray.sort(), "<br />");
document.write(asciiArray.SortAsciiStringOrderByAscending(), "<br />");
document.write(asciiArray.SortAsciiStringOrderByDescending(), "<br />");
document.write("<hr />");

var chineseArray = new Array("大", "中", "小", "啊", "喝");
document.write(chineseArray, "<br />");
document.write(chineseArray.sort(), "<br />");
document.write(chineseArray.SortChineseStringOrderByAscending(), "<br />");
document.write(chineseArray.SortChineseStringOrderByDescending(), "<br />");
document.write("<hr />");

//-->
</script>
</body>
</html>
``````
shan1119 2010-02-04
• 打赏
• 举报

``````var obj=[["d",2,new Date("2010/02/01")],
["c",3,new Date("2010/05/01")],
["a",4,new Date("2010/03/01")],
["b",1,new Date("2010/04/01")]];
var sortBy=0;
var flag=false;//asc:true;Desc:false
obj.sort(function(o1,o2){
if(o1[sortBy]>=o2[sortBy]==flag){return 1;}
else{return -1}
});
shan1119 2010-02-04
• 打赏
• 举报

``````
var obj=[["d",2,new Date("2010/02/01")],
["c",3,new Date("2010/05/01")],
["a",4,new Date("2010/03/01")],
["b",1,new Date("2010/04/01")]];
var sortBy=0;
var flag=false;//asc:true;Desc:false
obj.sort(function(o1,o2){
if(o1[sortBy]>=o2[sortBy]==asc){return 1;}
else{return -1}
});
licip 2010-02-04
• 打赏
• 举报

yixianggao 2010-02-04
• 打赏
• 举报

[Quote=引用 3 楼 wuyan0319 的回复:]

http://www.w3school.com.cn/js/jsref_sort.asp

[/Quote]

• 打赏
• 举报

• 打赏
• 举报

var s=new Array();
s[0]=["说明1",new Date(20),3];
s[1]=["2说明",new Date(256),110];
s[2]=["我来说明",new Date(10),20];
var re= s.sort(function(o1,o2){
if(o1[1]>o2[1])/**按日期升序排列，其它类推*/
{
return 1;
}
else if(o1[1]==o2[1])
{
return -1;
}
else return 1;

});
Jack Wang 2010-02-03
• 打赏
• 举报

[Quote=引用 1 楼 ltnrain 的回复:]
http://www.w3school.com.cn/js/jsref_sort.asp
[/Quote]

jiewenxu 2010-02-03
• 打赏
• 举报

arrayObject.sort(sortby)

sortby 可选。规定排序顺序。必须是函数。

• 打赏
• 举报

http://www.w3school.com.cn/js/jsref_sort.asp

8.7w+

Web 开发 JavaScript

2010-02-03 06:25