如何在javascript中按时间对对象数组进行排序

时间:2017-01-16 09:57:41

标签: javascript arrays api sorting time

我有一个像这样的对象数组:

  • [0] = {transportnumber:'45',时间:'10:28:00',日期:“2017-01-16”}
  • [1] = {transportnumber:'45',时间:'10:38:00',日期:“2017-01-16”}
  • [2] = {transportnumber:'45',时间:'10:48:00',日期:“2017-01-16”}
  • [3] = {transportnumber:'14',时间:'10:12:00',日期:“2017-01-16”}
  • [4] = {transportnumber:'14',时间:'10:24:00',日期:“2017-01-16”}
  • [5] = {transportnumber:'14',时间:'10:52:00',日期:“2017-01-16”}

对象数组总是看起来像这样未排序。首先是运输编号,然后是时间。这是由于api我正在使用。

我的问题是:我如何只按时间排序这个数组?

我尝试在我的变量中使用sort函数,如下所示,其中存储了对象数组但没有成功:

allBuses.sort(function(a,b){
var c = a.time;
var d = b.time;

if(c > d){
return d
}

else return c

8 个答案:

答案 0 :(得分:3)

您可以将time视为字符串,并使用String#localeCompare进行排序。

var data = [{ transportnumber: '45', time: '10:28:00', date:"2017-01-16"}, { transportnumber: '45', time: '10:38:00', date:"2017-01-16" },{ transportnumber: '45', time: '10:48:00', date:"2017-01-16" }, { transportnumber: '14', time: '10:12:00', date:"2017-01-16" }, { transportnumber: '14', time: '10:24:00', date:"2017-01-16" }, { transportnumber: '14', time: '10:52:00', date:"2017-01-16"}];

data.sort(function (a, b) {
    return a.time.localeCompare(b.time);
});

console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:1)

    var data = [{ transportnumber: '45', time: '10:28:00', date:"2017-01-16"}, { transportnumber: '45', time: '10:38:00', date:"2017-01-16" },{ transportnumber: '45', time: '10:48:00', date:"2017-01-16" }, { transportnumber: '14', time: '10:12:00', date:"2017-01-16" }, { transportnumber: '14', time: '10:24:00', date:"2017-01-16" }, { transportnumber: '14', time: '10:52:00', date:"2017-01-16"}];

data.sort(function(a,b){

return b.time>a.time; 


})
console.log(data)

答案 2 :(得分:0)

您将返回 c d ,这是时间值。你的职能需要回归......

    如果值相同,则
  • 0
  • 如果 b 低于 a (例如1)
  • ,则为正(> 0)
  • 否定(< 0)如果 a ,如果低于 b (例如-1)

答案 3 :(得分:0)

var array = [ { transportnumber: '45', time: '10:28:00', date:"2017-01-16"}
,{ transportnumber: '45', time: '10:38:00', date:"2017-01-16"}
,{ transportnumber: '45', time: '10:48:00', date:"2017-01-16"}
,{ transportnumber: '14', time: '10:12:00', date:"2017-01-16"}
,{ transportnumber: '14', time: '10:24:00', date:"2017-01-16"}
,{ transportnumber: '14', time: '10:52:00', date:"2017-01-16"}];
array.sort(function(a,b){
  return new Date(b.date + " "+b.time) - new Date(a.date + " "+a.time);
});
console.log(array);

您可以将日期和时间结合起来并按此排序

答案 4 :(得分:0)

您可以使用Array.prototype.sort()

var allBuses = [{ transportnumber: '45', time: '10:28:00', date:"2017-01-16"}, { transportnumber: '45', time: '10:38:00', date:"2017-01-16" },{ transportnumber: '45', time: '10:48:00', date:"2017-01-16" }, { transportnumber: '14', time: '10:12:00', date:"2017-01-16" }, { transportnumber: '14', time: '10:24:00', date:"2017-01-16" }, { transportnumber: '14', time: '10:52:00', date:"2017-01-16"}];

allBuses.sort((a, b) => (a.time < b.time) ? -1 : ((a.time > b.time) ? 1 : 0));

console.log(allBuses);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 5 :(得分:0)

如果您还想考虑添加日期

data = []
data[0] = { transportnumber: '45', time: '10:28:00', date:"2017-01-16"}
data[1] = { transportnumber: '45', time: '10:38:00', date:"2017-01-16"}
data[2] = { transportnumber: '45', time: '10:48:00', date:"2017-01-16"}
data[3] = { transportnumber: '14', time: '10:12:00', date:"2017-01-16"}
data[4] = { transportnumber: '14', time: '10:24:00', date:"2017-01-16"}
data[5] = { transportnumber: '14', time: '10:52:00', date:"2017-01-16"}

data.sort((a, b) => {
    if (a.date < b.date)
        return -1

    if (a.date > b.date)
        return 1

    if (a.date == b.date) {
        if (a.time < b.time)
            return -1

        if (a.time > b.time)
            return 1

        return 0
    }
});

答案 6 :(得分:0)

试试这个

    allBuses.sort(function(a,b){
    var c = new Date(a.date + ":"+ a.time).getTime();
    var d = new Date(b.date + ":"+b.time).getTime();
    console.log(c,d);
    return c -d;
    });

答案 7 :(得分:0)

如果您的应用程序有lodash,则可以非常轻松地完成。

var sorted = _.orderBy(data,function(item){
return new Date(item.date + " " + data.time);
});