问题
I have a object array looking something like this:
- [0] = { transportnumber: '45', time: '10:28:00', date:"2017-01-16"}
- [1] = { transportnumber: '45', time: '10:38:00', date:"2017-01-16"}
- [2] = { transportnumber: '45', time: '10:48:00', date:"2017-01-16"}
- [3] = { transportnumber: '14', time: '10:12:00', date:"2017-01-16"}
- [4] = { transportnumber: '14', time: '10:24:00', date:"2017-01-16"}
- [5] = { transportnumber: '14', time: '10:52:00', date:"2017-01-16"}
The object array will always look like this unsorted. First by transport number and then by time. This due to the api i´m using.
My question is: How i can sort this array only by time?
I´ve tried using the sort function as seen below on my variable where the object array is stored but with no success:
allBuses.sort(function(a,b){
var c = a.time;
var d = b.time;
if(c > d){
return d
}
else return c
回答1:
You could treat time
as string and sort with 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; }
回答2:
You can use 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; }
回答3:
You're returning c or d, which are the time values. Your function needs to return...
- 0 if the values are the same
- Positive (>0) if b is lower than a (eg. 1)
- Negative (<0) if a if lower than b (eg. -1)
回答4:
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);
You could combine both date and time and sort like this
回答5:
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)
回答6:
In case if you want to add date also in consideration
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
}
});
回答7:
Try this
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;
});
回答8:
If your application has lodash, you can do it very easily.
var sorted = _.orderBy(data,function(item){
return new Date(item.date + " " + data.time);
});
来源:https://stackoverflow.com/questions/41673669/how-to-sort-object-array-by-time-in-javascript