子组内的JS数组排序

时间:2018-06-14 14:35:54

标签: javascript jquery

我有这个JS数组......

[{
  "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"
}]

这是我的代码......

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

我希望在这个阵列上进行2次操作......

  1. 将此数组分组为" all_day_evt"键。我可以轻松地使用Func 1来做到这一点。
  2. 将此数组排在' true'小组,' false'子组由' Start_time'。我可以使用Func 2轻松完成此操作,但仅适用于所有整个组/阵列。
  3. 我如何在子组中执行此操作,即' true'和'假'' ?我在组合这两个功能时遇到了一些困难。

    任何建议都非常感谢。感谢。

2 个答案:

答案 0 :(得分:1)

您需要将两个字段一起排序。试试以下



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




答案 1 :(得分:1)

您可以根据两种情况对数组进行排序,仅限单个比较器。

尝试以下方法:



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