14

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
2
  • You're misusing sort(). Have you read its documentation? Commented Jan 16, 2017 at 10:00
  • 1
    change your whole if statement with return c > d ? 1 : -1 Commented Jan 16, 2017 at 10:05

8 Answers 8

34

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; }

Sign up to request clarification or add additional context in comments.

Comments

5

You can use Array.prototype. toSorted() and destructuring assignment to sort comparing time strings:

const 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"}]

const sortedAllBuses = allBuses.toSorted(
  ({ time: a }, { time: b }) => a < b ? -1 : a > b ? 1 : 0
)

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

Comments

1

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
    }
});

Comments

0

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)

Comments

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);

You could combine both date and time and sort like this

Comments

0
    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 Comments

Check that the Array.prototype.sort() function can simply subtract b from a if you are comparing Numbers.. but in this case you are comparing Strings
This will not work correctly. The compare function should return a value that is either less than zero, zero, or more than zero. This will only return true or false, which is effectively 1 or 0 (so no negative numbers).
0

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;
    });

1 Comment

That will create a string like "2017-01-16:10:28:00" which is not consistent with the format supported in ECMA-262 and will very likely not be parsed correctly in most implementations (such as Safari and Firefox). It would be better to join with "T", but that too is somewhat problematic.
0

If your application has lodash, you can do it very easily.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.