2

I have this JS Array ....

[{
  "Event_code": "BW-114",
  "Interest_area": "Nursing and midwifery",
  "Start_time": "9:00 AM",
  "End_time": "3:00 PM",
  "Session_type": "Tour",
  "all_day_evt": true
}, {
  "Event_code": "BW-087",
  "Interest_area": "Information technology",
  "Start_time": "9:00 AM",
  "End_time": "3:00 PM",
  "Session_type": "Experience",
  "all_day_evt": true
}, {
  "Event_code": "BW-060",
  "Interest_area": "Sport",
  "Start_time": "9:30 AM",
  "End_time": "3:00 PM",
  "Session_type": "Tour",
  "all_day_evt": true
}, {
  "Event_code": "BW-115",
  "Interest_area": "Food, Nutrition and dietetics",
  "Start_time": "9:30 AM",
  "End_time": "3:00 PM",
  "Session_type": "Tour",
  "all_day_evt": true
}, {
  "Event_code": "BW-161",
  "Interest_area": "Media, Communication and creative arts",
  "Start_time": "9:00 AM",
  "End_time": "3:00 PM",
  "Session_type": "Experience",
  "all_day_evt": true
}, {
  "Event_code": "BW-032",
  "Interest_area": "",
  "Start_time": "9:30 AM",
  "End_time": "10:00 AM",
  "Session_type": "General information session",
  "all_day_evt": false
}, {
  "Event_code": "BW-129",
  "Interest_area": "Media, Communication and creative arts",
  "Start_time": "2:00 PM",
  "End_time": "2:30 PM",
  "Session_type": "Tour",
  "all_day_evt": false
}, {
  "Event_code": "BW-081",
  "Interest_area": "Information technology",
  "Start_time": "9:00 AM",
  "End_time": "9:30 AM",
  "Session_type": "Course information session",
  "all_day_evt": false
}, {
  "Event_code": "BW-048",
  "Interest_area": "Media, Communication and creative arts",
  "Start_time": "12:00 PM",
  "End_time": "12:30 PM",
  "Session_type": "Experience",
  "all_day_evt": false
}, {
  "Event_code": "BW-128",
  "Interest_area": "Media, Communication and creative arts",
  "Start_time": "12:00 PM",
  "End_time": "12:30 PM",
  "Session_type": "Tour",
  "all_day_evt": false,
  "clash": "This clashes with another session"
}, {
  "Event_code": "BW-039",
  "Interest_area": "Media, Communication and creative arts",
  "Start_time": "1:00 PM",
  "End_time": "1:30 PM",
  "Session_type": "Experience",
  "all_day_evt": false
}, {
  "Event_code": "BW-162",
  "Interest_area": "Education and teaching",
  "Start_time": "1:00 PM",
  "End_time": "1:30 PM",
  "Session_type": "Tour",
  "all_day_evt": false,
  "clash": "This clashes with another session"
}, {
  "Event_code": "BW-034",
  "Interest_area": "",
  "Start_time": "1:30 PM",
  "End_time": "2:00 PM",
  "Session_type": "General information session",
  "all_day_evt": false
}, {
  "Event_code": "BW-170",
  "Interest_area": "",
  "Start_time": "9:30 AM",
  "End_time": "10:30 AM",
  "Session_type": "General information session",
  "all_day_evt": false,
  "clash": "This clashes with another session"
}]

Here is my code ...

// Func 1 -- Sort by 'all_day_evt' key i.e. True/False

arr.sort(function (x, y) {
  return y.all_day_evt - x.all_day_evt;
});
console.log(arr);

//  Func 2 -- Sort by time

arr.sort(function (a, b) {
  return ((new Date('1970/01/01 ' + a.Start_time)) - (new Date('1970/01/01 ' + b.Start_time)));
});
console.log(arr);

I am looking to do 2 operations on this array ...

  1. Group this array by "all_day_evt" key. I can do this using Func 1 easily.
  2. Sort this array within the 'true' subgroup, and 'false' subgroup by 'Start_time'. I can do this using Func 2 easily, but only on all whole group/array.

How do I do this within the subgroups i.e. group of 'true' and group of 'false' ? I am having some difficulties combining these 2 functions.

Any advise is very appreciated. Thanks.

1
  • 2
    Have you tried combining your sorts into a single method, so that the second conditional happens if the first is 0? Commented Jun 14, 2018 at 14:38

2 Answers 2

1

You need to sort by both the fields together. Try following

let arr = [{"Event_code":"BW-114","Interest_area":"Nursing and midwifery","Start_time":"9:00 AM","End_time":"3:00 PM","Session_type":"Tour","all_day_evt":true},{"Event_code":"BW-087","Interest_area":"Information technology","Start_time":"9:00 AM","End_time":"3:00 PM","Session_type":"Experience","all_day_evt":true},{"Event_code":"BW-060","Interest_area":"Sport","Start_time":"9:30 AM","End_time":"3:00 PM","Session_type":"Tour","all_day_evt":true},{"Event_code":"BW-115","Interest_area":"Food, Nutrition and dietetics","Start_time":"9:30 AM","End_time":"3:00 PM","Session_type":"Tour","all_day_evt":true},{"Event_code":"BW-161","Interest_area":"Media, Communication and creative arts","Start_time":"9:00 AM","End_time":"3:00 PM","Session_type":"Experience","all_day_evt":true},{"Event_code":"BW-032","Interest_area":"","Start_time":"9:30 AM","End_time":"10:00 AM","Session_type":"General information session","all_day_evt":false},{"Event_code":"BW-129","Interest_area":"Media, Communication and creative arts","Start_time":"2:00 PM","End_time":"2:30 PM","Session_type":"Tour","all_day_evt":false},{"Event_code":"BW-081","Interest_area":"Information technology","Start_time":"9:00 AM","End_time":"9:30 AM","Session_type":"Course information session","all_day_evt":false},{"Event_code":"BW-048","Interest_area":"Media, Communication and creative arts","Start_time":"12:00 PM","End_time":"12:30 PM","Session_type":"Experience","all_day_evt":false},{"Event_code":"BW-128","Interest_area":"Media, Communication and creative arts","Start_time":"12:00 PM","End_time":"12:30 PM","Session_type":"Tour","all_day_evt":false,"clash":"This clashes with another session"},{"Event_code":"BW-039","Interest_area":"Media, Communication and creative arts","Start_time":"1:00 PM","End_time":"1:30 PM","Session_type":"Experience","all_day_evt":false},{"Event_code":"BW-162","Interest_area":"Education and teaching","Start_time":"1:00 PM","End_time":"1:30 PM","Session_type":"Tour","all_day_evt":false,"clash":"This clashes with another session"},{"Event_code":"BW-034","Interest_area":"","Start_time":"1:30 PM","End_time":"2:00 PM","Session_type":"General information session","all_day_evt":false},{"Event_code":"BW-170","Interest_area":"","Start_time":"9:30 AM","End_time":"10:30 AM","Session_type":"General information session","all_day_evt":false,"clash":"This clashes with another session"}];

arr.sort((x,y) => y.all_day_evt- x.all_day_evt || ((new Date('1970/01/01 ' + x.Start_time)) - (new Date('1970/01/01 ' + y.Start_time))));
console.log(arr);

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

7 Comments

Nopes, didn't quite worked @NikhilAggarwal The 6th event 'BW-081' in your result is 9:00 AM and its is wrongly placed. It came after the 9:30 AM event.
@Slyper - It is the first object with false value for all_day_evt. I find it to be at right place. Can you share what exactly went wrong for you?
Sorry, my bad. You are right @NikhilAggarwal. Thanks again for your help mate.
@Slyper - Welcome mate :)
Hey @NikhilAggarwal, Can you help me this question please ?
|
1

You can sort your array based on both the conditions, in single comparator only.

Try the following:

var arr = [{"Event_code":"BW-114","Interest_area":"Nursing and midwifery","Start_time":"9:00 AM","End_time":"3:00 PM","Session_type":"Tour","all_day_evt":true},{"Event_code":"BW-087","Interest_area":"Information technology","Start_time":"9:00 AM","End_time":"3:00 PM","Session_type":"Experience","all_day_evt":true},{"Event_code":"BW-060","Interest_area":"Sport","Start_time":"9:30 AM","End_time":"3:00 PM","Session_type":"Tour","all_day_evt":true},{"Event_code":"BW-115","Interest_area":"Food, Nutrition and dietetics","Start_time":"9:30 AM","End_time":"3:00 PM","Session_type":"Tour","all_day_evt":true},{"Event_code":"BW-161","Interest_area":"Media, Communication and creative arts","Start_time":"9:00 AM","End_time":"3:00 PM","Session_type":"Experience","all_day_evt":true},{"Event_code":"BW-032","Interest_area":"","Start_time":"9:30 AM","End_time":"10:00 AM","Session_type":"General information session","all_day_evt":false},{"Event_code":"BW-129","Interest_area":"Media, Communication and creative arts","Start_time":"2:00 PM","End_time":"2:30 PM","Session_type":"Tour","all_day_evt":false},{"Event_code":"BW-081","Interest_area":"Information technology","Start_time":"9:00 AM","End_time":"9:30 AM","Session_type":"Course information session","all_day_evt":false},{"Event_code":"BW-048","Interest_area":"Media, Communication and creative arts","Start_time":"12:00 PM","End_time":"12:30 PM","Session_type":"Experience","all_day_evt":false},{"Event_code":"BW-128","Interest_area":"Media, Communication and creative arts","Start_time":"12:00 PM","End_time":"12:30 PM","Session_type":"Tour","all_day_evt":false,"clash":"This clashes with another session"},{"Event_code":"BW-039","Interest_area":"Media, Communication and creative arts","Start_time":"1:00 PM","End_time":"1:30 PM","Session_type":"Experience","all_day_evt":false},{"Event_code":"BW-162","Interest_area":"Education and teaching","Start_time":"1:00 PM","End_time":"1:30 PM","Session_type":"Tour","all_day_evt":false,"clash":"This clashes with another session"},{"Event_code":"BW-034","Interest_area":"","Start_time":"1:30 PM","End_time":"2:00 PM","Session_type":"General information session","all_day_evt":false},{"Event_code":"BW-170","Interest_area":"","Start_time":"9:30 AM","End_time":"10:30 AM","Session_type":"General information session","all_day_evt":false,"clash":"This clashes with another session"}];
arr.sort(function (x, y) {
  return (y.all_day_evt - x.all_day_evt) || ((new Date('1970/01/01 ' + x.Start_time)) - (new Date('1970/01/01 ' + y.Start_time)));
});
console.log(arr);

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.