2

I have an array of objects that is like this:

var array = [
    {minutes: 45, hours: 4, meridiem: "PM"},
    {minutes: 00, hours: 8, meridiem: "AM"},
    {minutes: 45, hours: 8, meridiem: "AM"},
    {minutes: 30, hours: 3, meridiem: "PM"},
    {minutes: 15, hours: 11, meridiem: "AM"},
    {minutes: 00, hours: 1, meridiem: "PM"}
]

Essentially I need to sort by time, earliest first. So by AM/PM then by hours and then by minutes if hours and meridiem are equal.

Here is what I've tried

this.currentTours.sort(function(a,b) {
  if (a.meridiem != b.meridiem) {
    return 1
  }
  if ((a.dateR.meridiem == b.dateR.meridiem) && a.dateR.hours != 
  b.dateR.hours) {
    return 1
  }
  if (((a.dateR.meridiem == b.dateR.meridiem) && a.dateR.hours != 
  b.dateR.hours) && a.dateR.minutes != b.dateR.minutes) {
    return 1
  }
  return a.dateR.meridiem - b.dateR.meridiem
})

Getting past 1 layer of properties is where I get stuck.

5
  • why not use a 24 hour system? Commented Sep 14, 2017 at 17:43
  • Show us what you already tried so we can help you to handle it. This is not a site where people do your work for you. Commented Sep 14, 2017 at 17:48
  • 1
    I know @voloshin thank you. Commented Sep 14, 2017 at 17:53
  • I think this -- How do you sort an array of objects based on multiple properties -- is a really good question, but it's unfortunate that time is the example in use. It begs the question, as Nina says, why not just a 24 hour integer? {minutes: 45, hours: 4, meridiem: "PM"} is 1645 while {minutes: 17, hours: 9, meridiem: "AM"} is 945. Sorting that is trivial. An example with, say, {name:"Fred", age:27, country:"GBR"} where you want to sort by Country, then Age, then Name doesn't raise "Why don't you just X?" questions. Commented Sep 14, 2017 at 21:21
  • I think using a 24hr system is a good recommendation. Thanks. Commented Sep 14, 2017 at 21:37

2 Answers 2

3

You could use a chained approach for a nested sorting, with a hour correction for 12 am or pm. These values are interpreted as zero.

(a.hours < 12) * a.hours

var array = [{ minutes: 45, hours: 4, meridiem: "PM" }, { minutes: 00, hours: 8, meridiem: "AM" }, { minutes: 45, hours: 8, meridiem: "AM" }, { minutes: 30, hours: 3, meridiem: "PM" }, { minutes: 15, hours: 11, meridiem: "AM" }, { minutes: 00, hours: 1, meridiem: "PM" }, { minutes: 00, hours: 12, meridiem: "PM" },{ minutes: 00, hours: 12, meridiem: "AM" }];

array.sort(function (a, b) {
    return (b.meridiem === 'AM') - (a.meridiem === 'AM')
        || (a.hours < 12) * a.hours - (b.hours < 12) * b.hours
        || a.minutes - b.minutes;
});

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

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

Comments

1

You can use simple if statements in the Array.prototype.sort method to take multiple properties into account.

If two properties are exactly equal, consult the next most substantial property to break such ties.

var array = [
  { minutes: 45, hours: 4, meridiem: "PM" },
  { minutes: 00, hours: 8, meridiem: "AM" },
  { minutes: 45, hours: 8, meridiem: "AM" },
  { minutes: 30, hours: 3, meridiem: "PM" },
  { minutes: 15, hours: 11, meridiem: "AM" },
  { minutes: 00, hours: 1, meridiem: "PM" }
];

array.sort(function(a, b) {
  if (a.meridiem !== b.meridiem)
    return a.meridiem === 'AM' ? -1 : 1;
  
  if (a.hours !== b.hours)
    return a.hours - b.hours;
  
  return a.minutes - b.minutes
});

console.log(array);

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.