/*
@desc:表格排序类
@param data 待排序数组
*/
function tablesort(data){
this.data = data
this.sortby = true
/*
@desc:主方法,排序
@param item 排序字段
@return ret 排序后的数据
*/
this.sort = function(item){
if(this.sortby){
var ret = this.data.sort(function(a,b){
return a[item]>b[item]
})
}else{
var ret = this.data.sort(function(a,b){
return a[item]<b[item]
})
}
this.sortby = !this.sortby
return ret
}
}
var data = new Array(
{
id:1,
title:'title1',
content:'content1'
},
{
id:3,
title:'title2',
content:'content3'
},
{
id:2,
title:'title3',
content:'content2'
}
)
var tablesort = new tablesort(data)
var ret = tablesort.sort('id')
console.log(ret)
var ret = tablesort.sort('title')
console.log(ret)
var ret = tablesort.sort('id')
console.log(ret)
[ { id: 1, title: 'title1', content: 'content1' },
{ id: 2, title: 'title3', content: 'content2' },
{ id: 3, title: 'title2', content: 'content3' } ]
[ { id: 2, title: 'title3', content: 'content2' },
{ id: 3, title: 'title2', content: 'content3' },
{ id: 1, title: 'title1', content: 'content1' } ]
[ { id: 1, title: 'title1', content: 'content1' },
{ id: 2, title: 'title3', content: 'content2' },
{ id: 3, title: 'title2', content: 'content3' } ]
网站栏目:javascript数组排序函数
网页URL:https://www.cdcxhl.com/article20/ghheco.html
成都网站建设公司_创新互联,为您提供品牌网站制作、微信公众号、网站设计、品牌网站建设、网站营销、搜索引擎优化
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联