js数组的sort排序详解
2016-03-08 23:55
591 查看
sort()对数组排序,不开辟新的内存,对原有数组元素进行调换
1、简单数组简单排序
<script type="text/javascript">
var arrSimple=new Array(1,8,7,6);
arrSimple.sort();
document.writeln(arrSimple.join());
</script>
2、简单数组自定义排序
<script type="text/javascript">
var arrSimple2=new Array(1,8,7,6);
arrSimple2.sort(function(a,b){
return b-a});
document.writeln(arrSimple2.join());
</script>简化一下:a-b输出从小到大排序,b-a输出从大到小排序。
3、简单对象List自定义属性排序
<script type="text/javascript">
var objectList = new Array();
function Persion(name,age){
this.name=name;
this.age=age;
}
objectList.push(new Persion('jack',20));
objectList.push(new Persion('tony',25));
objectList.push(new Persion('stone',26));
objectList.push(new Persion('mandy',23));
//按年龄从小到大排序
objectList.sort(function(a,b){
return a.age-b.age});
for(var i=0;i<objectList.length;i++){
document.writeln('<br />age:'+objectList[i].age+' name:'+objectList[i].name);
}
</script>
4、简单对象List对可编辑属性的排序
<script type="text/javascript">
var objectList2 = new Array();
function WorkMate(name,age){
this.name=name;
var _age=age;
this.age=function(){
if(!arguments)
{
_age=arguments[0];}
else
{
return _age;}
}
}
objectList2.push(new WorkMate('jack',20));
objectList2.push(new WorkMate('tony',25));
objectList2.push(new WorkMate('stone',26));
objectList2.push(new WorkMate('mandy',23));
//按年龄从小到大排序
objectList2.sort(function(a,b){
return a.age()-b.age();
});
for(var i=0;i<objectList2.length;i++){
document.writeln('<br />age:'+objectList2[i].age()+' name:'+objectList2[i].name);
}
</script>
1、简单数组简单排序
<script type="text/javascript">
var arrSimple=new Array(1,8,7,6);
arrSimple.sort();
document.writeln(arrSimple.join());
</script>
2、简单数组自定义排序
<script type="text/javascript">
var arrSimple2=new Array(1,8,7,6);
arrSimple2.sort(function(a,b){
return b-a});
document.writeln(arrSimple2.join());
</script>简化一下:a-b输出从小到大排序,b-a输出从大到小排序。
3、简单对象List自定义属性排序
<script type="text/javascript">
var objectList = new Array();
function Persion(name,age){
this.name=name;
this.age=age;
}
objectList.push(new Persion('jack',20));
objectList.push(new Persion('tony',25));
objectList.push(new Persion('stone',26));
objectList.push(new Persion('mandy',23));
//按年龄从小到大排序
objectList.sort(function(a,b){
return a.age-b.age});
for(var i=0;i<objectList.length;i++){
document.writeln('<br />age:'+objectList[i].age+' name:'+objectList[i].name);
}
</script>
4、简单对象List对可编辑属性的排序
<script type="text/javascript">
var objectList2 = new Array();
function WorkMate(name,age){
this.name=name;
var _age=age;
this.age=function(){
if(!arguments)
{
_age=arguments[0];}
else
{
return _age;}
}
}
objectList2.push(new WorkMate('jack',20));
objectList2.push(new WorkMate('tony',25));
objectList2.push(new WorkMate('stone',26));
objectList2.push(new WorkMate('mandy',23));
//按年龄从小到大排序
objectList2.sort(function(a,b){
return a.age()-b.age();
});
for(var i=0;i<objectList2.length;i++){
document.writeln('<br />age:'+objectList2[i].age()+' name:'+objectList2[i].name);
}
</script>
相关文章推荐
- JQuery1——基础($对象,选择器,对象转换)
- Android学习笔记(二九):嵌入浏览器
- Android java 与 javascript互访(相互调用)的方法例子
- JavaScript演示排序算法
- 在命令行用 sort 进行排序
- javascript实现10进制转为N进制数
- 最后一次说说闭包
- Ajax
- 2019年开发人员应该学习的8个JavaScript框架
- Linux Shell - 如何使用sort与uniq命令删除重复的文本行
- HTML中的script标签研究
- 对一个分号引发的错误研究
- 异步流程控制:7 行代码学会 co 模块
- ES6 走马观花(ECMAScript2015 新特性)
- JavaScript拆分字符串时产生空字符的原因
- Canvas 在高清屏下绘制图片变模糊的解决方法
- Redux系列02:一个炒鸡简单的react+redux例子