使用javascript按ID排序列表

使用javascript按ID排序列表

本文介绍了使用javascript按ID排序列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在iOS上制作一个phonegap应用程序,需要按时间排序列表
我尝试将时间添加到每个li项目的ID,然后根据id进行排序

I'm making a phonegap app on iOS that requires sort a list by timeI tried add the time to the id of each li item and then sort based on the 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>

这里是javascript:

and here is the 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);
});

我尝试使用chrome并且运行良好。但是,如果我尝试使用phonegap应用程序,则列表未正确排序。它没有遵循任何顺序。对此有何解决方案?
P / s:有人说在Safari上它应该是(新日期(a.id) - 新日期(b.id))在Safari上,但似乎它不会影响phonegap

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

详细了解我的手机密码。此代码从db中检索记录并将其显示为html上的项目列表。

Explain more about my phonegap code . This code retrive records from db and show it as list of items on 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');

}


推荐答案

比较函数必须返回一个整数。 检查它是否为负数,零或正数,并使用它来确定应如何排序这两个元素。所以:

The comparison function must return an integer. sort checks whether it's negative, zero, or positive, and uses that to determine how the two elements should be ordered. So do:

elems.sort(function(a, b) {
    return a.id - b.id;
});

或者,在现代:

elems.sort((a, b) => a.id - b.id);

这篇关于使用javascript按ID排序列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 12:30