首页 > 解决方案 > JS 数组,布尔排序不起作用

问题描述

这是我的代码...

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

排序不起作用。运行代码,你会看到。有人可以告诉我代码有什么问题吗?谢谢

标签: javascriptjquery

解决方案


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

x - y // false first
y - x // true first

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


推荐阅读