使用 javascript 按 id 对列表进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23661115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sort a list by id by using javascript
提问by ThangPQ
I'm making a phonegap app on iOS that requires sort a list by time I tried add the time to the id of each li item and then sort based on the id
我正在 iOS 上制作一个 phonegap 应用程序,它需要按时间对列表进行排序,我尝试将时间添加到每个 li 项目的 id 中,然后根据 id 进行排序
<ul id="test">
<li id="4112">blub</li>
<li id="1422">blaaah</li>
<li id="6640">hmmmm</li>
<li id="2221">one more</li>
</ul>
and here is the javascript:
这是javascript:
$(function(){
var elems = $('#test').children('li').remove();
elems.sort(function(a,b){
return (new Date(a.id) > new Date(b.id));
});
$('#test').append(elems);
});
http://jsfiddle.net/3uYUq/1103/
http://jsfiddle.net/3uYUq/1103/
I tried on chrome and it ran well . However, if I tried on phonegap app, the list is not sorted correctly . It doesn't follow any order. Any solution for this ? P/s: Someone said that on Safari it should be (new Date(a.id) - new Date(b.id)) on Safari but seems that it doesn't affect phonegap
我试过 chrome,它运行良好。但是,如果我尝试使用 phonegap 应用程序,则列表未正确排序。它不遵循任何顺序。对此有什么解决方案吗?P/s:有人说在 Safari 上它应该是 (new Date(a.id) - new Date(b.id)) 在 Safari 上,但似乎它不影响 phonegap
Explain more about my phonegap code . This code retrive records from db and show it as list of items on html.
详细解释一下我的 phonegap 代码。此代码从 db 检索记录并将其显示为 html 上的项目列表。
function getAllDeadlines_success(tx, results){
var len = results.rows.length;
//var s = "";
$('#allList').empty();
var tmpDueDate = '1900-01-01';
var tmpDueTime = '00:00';
for (var i=0; i<len; i++){
var allDeadline = results.rows.item(i);
var deadlineDatePart = allDeadline.duedate.split('-');
var deadlineTimePart = allDeadline.duetime.split(':');
var newDate = new Date(deadlineDatePart[0], deadlineDatePart[1] - 1 , deadlineDatePart[2], deadlineTimePart[0], deadlineTimePart[1], 0, 0);
var notiDate = new Date(newDate - 86400*1000);
//compare with current time
var result = isLate(allDeadline.duedate, allDeadline.duetime).toString();
if ( result == "true"){
$('#allList').append('<li id = "'+allDeadline.duedate+' '+allDeadline.duetime+'"><a href="#DeadlineDetail" id = "'+allDeadline.id+'" data-transition = "slide">'+ allDeadline.class +'<br>'+ allDeadline.duedate+' '+ allDeadline.duetime+'<br>'+ allDeadline.description +'</a></li>');
// window.plugin.notification.local.add({
// id : getRandomInt(0,99999),
// message: 'Dont forget to complete: '+allDeadline.description+'',
// badge: 0,
// date: notiDate
// });
}
}
$(function(){
var elems = $('#allList').children('li').remove();
elems.sort(function(a,b){
return (new Date(a.id) > new Date(b.id));
});
$('#allList').append(elems);
});
$("#allList").listview().listview('refresh');
}
回答by Barmar
The comparison function must return an integer. sortchecks whether it's negative, zero, or positive, and uses that to determine how the two elements should be ordered. So do:
比较函数必须返回一个整数。sort检查它是负数、零还是正数,并使用它来确定两个元素应该如何排序。所以这样做:
elems.sort(function(a, b) {
return a.id - b.id;
});
Or, in modern ES6 style:
或者,在现代ES6 风格中:
elems.sort((a, b) => a.id - b.id);

