0

This is the structure of a JSON file I have been given once it is returned and then passed into an array. I am attempting to sort by date.

var model = [ {id: "1", date: "2018-05-08", racedistance: "10000", race_name: "Riverside Park", venue_name: "Riverside Park", …},
{id: "140", date: "2018-05-01", racedistance: "42195", race_name: "Placeholder Race Name 1", venue_name: "Riverside Park", …},
{id: "149", date: "2018-05-10", racedistance: "10000", race_name: "Placeholder Race Name 3", venue_name: "Riverside Park", …},
{id: "172", date: "2018-06-04", racedistance: "21097", race_name: "Placeholder Race Name 3", venue_name: "Riverside Park", …},
{id: "177", date: "2018-06-06", racedistance: "21097", race_name: "Placeholder Race Name 5", venue_name: "Riverside Park", …},
{id: "178", date: "2018-06-10", racedistance: "21097", race_name: "Placeholder Race Name 1", venue_name: "Riverside Park", …},
{id: "183", date: "2018-06-13", racedistance: "10000", race_name: "Placeholder Race Name 2", venue_name: "Riverside Park", …},
{id: "198", date: "2018-06-27", racedistance: "21097", race_name: "Placeholder Race Name 2", venue_name: "Riverside Park", …},
{id: "144", date: "2018-05-04", racedistance: "42195", race_name: "Placeholder Race Name 5", venue_name: "Albert Park", …}]

//This is the sort function I have been attempting to use.

function mycomparator(a,b) {
  return new Date(a.date, 10) - new Date(b.date, 10);
}
model.sort(mycomparator);

//This is how the array is returned.

{id: "1", date: "2018-05-08", racedistance: "10000", race_name: "Riverside Park", venue_name: "Riverside Park", …},
{id: "192", date: "2018-06-23", racedistance: "10000", race_name: "Placeholder Race Name 2", venue_name: "Saltwell Park to Chopwell Woods", …},
{id: "149", date: "2018-05-10", racedistance: "10000", race_name: "Placeholder Race Name 3", venue_name: "Riverside Park", …},
{id: "172", date: "2018-06-04", racedistance: "21097", race_name: "Placeholder Race Name 3", venue_name: "Riverside Park", …},
{id: "177", date: "2018-06-06", racedistance: "21097", race_name: "Placeholder Race Name 5", venue_name: "Riverside Park", …},
{id: "178", date: "2018-06-10", racedistance: "21097", race_name: "Placeholder Race Name 1", venue_name: "Riverside Park", …},
{id: "183", date: "2018-06-13", racedistance: "10000", race_name: "Placeholder Race Name 2", venue_name: "Riverside Park", …}

I would like to be sorted by date, earliest first.

Simon Dean
  • 227
  • 1
  • 4
  • 14
  • 2
    That ain't JSON. What's with the `10` in your `Date` constructors? – Phil Apr 29 '18 at 23:27
  • Not very clear what problem or question really is here. *"in some manner"* isn't very descriptive. Also not clear what expectations are – charlietfl Apr 29 '18 at 23:30
  • 2
    `new Date(a.date, 10)` yields `Invalid Date`. Please try using the [debugging capabilities](https://developer.mozilla.org/en-US/docs/Mozilla/Debugging/Debugging_JavaScript) of your browser. Just remove the `10`. – Sebastian Simon Apr 29 '18 at 23:32
  • 6
    Since the dates are in yyyy-mm-dd format, you can simply use `a.date.localeCompare(b.date)` -- string sorting will work. – Heretic Monkey Apr 29 '18 at 23:33
  • @MikeMcCaughan or even `-(a < b) || +(a > b)` since strings support inequality comparisons – Patrick Roberts Apr 29 '18 at 23:35
  • Thanks for the responses, I am just getting to grips with this today and is has been a long one... I am retrieving a JSON object and then assigning it to the array model. This is what I want to sort by date. – Simon Dean Apr 29 '18 at 23:37
  • @MikeMcCaughan that worked, thank you so much. – Simon Dean Apr 29 '18 at 23:40

1 Answers1

-1

var model = [ {id: "1", date: "2018-05-08", racedistance: "10000", race_name: "Riverside Park", venue_name: "Riverside Park"},
{id: "140", date: "2018-05-01", racedistance: "42195", race_name: "Placeholder Race Name 1", venue_name: "Riverside Park"},
{id: "149", date: "2018-05-10", racedistance: "10000", race_name: "Placeholder Race Name 3", venue_name: "Riverside Park"},
{id: "172", date: "2018-06-04", racedistance: "21097", race_name: "Placeholder Race Name 3", venue_name: "Riverside Park"},
{id: "177", date: "2018-06-06", racedistance: "21097", race_name: "Placeholder Race Name 5", venue_name: "Riverside Park"},
{id: "178", date: "2018-06-10", racedistance: "21097", race_name: "Placeholder Race Name 1", venue_name: "Riverside Park"},
{id: "183", date: "2018-06-13", racedistance: "10000", race_name: "Placeholder Race Name 2", venue_name: "Riverside Park"},
{id: "198", date: "2018-06-27", racedistance: "21097", race_name: "Placeholder Race Name 2", venue_name: "Riverside Park"},
{id: "144", date: "2018-05-04", racedistance: "42195", race_name: "Placeholder Race Name 5", venue_name: "Albert Park"}];



model.sort(function(a, b) {
    a = new Date(a.date);
    b = new Date(b.date);
    return a<b ? -1 : a>b ? 1 : 0;
});

for(var key in model){
 console.log(model[key].date);
}
mscdeveloper
  • 2,749
  • 1
  • 10
  • 17
  • This is very helpful, I find this harder to read than the previous suggestion, I think that is because I am unsure of exactly what the ternary operator is doing in the return statement. – Simon Dean Apr 29 '18 at 23:44
  • 1
    It's much simpler to just subtract one date from another (as OP was already attempting). Using a `for..in` loop on an array is also not a good idea – Phil Apr 29 '18 at 23:45