JS Array,布尔排序不起作用

时间:2018-06-13 08:38:44

标签: javascript jquery

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

var arr = [{"Event_code":"BW-033","Interest_area":"","Start_time":"9:00 AM","End_time":"3:00 PM","Session_type":"General information session","all_day_evt":true},{"Event_code":"BW-055","Interest_area":"","Start_time":"9:00 AM","End_time":"3:00 PM","Session_type":"General information session","all_day_evt":true},{"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-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-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-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-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"},{"Event_code":"BW-035","Interest_area":"Accelerate","Start_time":"12:00 PM","End_time":"12:30 PM","Session_type":"General information session","all_day_evt":false}]

function sort_arr(myarr){
       myarr.sort(function (x, y) {
          return x.all_day_evt - y.all_day_evt;
        });
}

sort_arr(arr);

排序无效。运行代码,你会看到。有人可以告诉代码中有什么问题吗?感谢

2 个答案:

答案 0 :(得分:1)

您可以减去布尔值并获取组中的所有false值和true值,具体取决于操作数的顺序:

function sort(array) {
    array.sort(function (x, y) {
        return x.all_day_evt - y.all_day_evt;
    });
}

var array = [{ Event_code: "BW-033", Interest_area: "", Start_time: "9:00 AM", End_time: "3:00 PM", Session_type: "General information session", all_day_evt: true }, { Event_code: "BW-055", Interest_area: "", Start_time: "9:00 AM", End_time: "3:00 PM", Session_type: "General information session", all_day_evt: true }, { 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-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-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-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-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" }, { Event_code: "BW-035", Interest_area: "Accelerate", Start_time: "12:00 PM", End_time: "12:30 PM", Session_type: "General information session", all_day_evt: false }]

sort(array);

console.log(array);



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

{{1}}




答案 1 :(得分:0)

是,然后是假:

arr.sort(function (x, y) {
  return x.all_day_evt < y.all_day_evt)
});

错误,然后是真

arr.sort(function (x, y) {
  return x.all_day_evt > y.all_day_evt)
});