javascript - 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);
排序不起作用。运行代码,你会看到。有人可以告诉我代码有什么问题吗?谢谢
解决方案
您可以减去布尔值并获得一组中的所有假值和真值,具体取决于操作数的顺序:
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; }
推荐阅读
- ruby-on-rails - 您可以根据枚举属性值执行包含验证吗?
- python - 如何检查变量是否不等于字符串
- mysql - MySQL LOCATE 与 Bigquery 的等价物是什么?
- python - Enhanced-dpy discord bot 在本地运行良好,但在 Heroku 中出现错误
- sql-server - SqlReader 读取时存储过程结果集存储在哪里?
- asp.net-core - 让 .NET 5 Web API 在 IIS 中一直运行
- r - 基于R中的其他数据集向一个数据集添加一行
- php - PHP“while”循环没有为每个循环执行第二个命令
- interrupt - 在 ISR 执行期间禁止在 esp32 上加载
- excel - 尝试从本地 Excel 表访问谷歌表单 gsheet 中的数据