javascript - 对象的Javascript动态排序数组在排序时包括空值
问题描述
我有对象数组。它可以包含字符串和整数。我想根据属性对其进行排序,
在其中一个排序顺序(asc/desc)中,空值应该排在第一位。
当数组中的属性不存在或为空时,它应该将其视为空。就像在某些元素中未定义年龄或缺少姓氏
数组的例子是这个
function dynamicsort(property,order) {
var sort_order = 1;
if(order === "desc"){
sort_order = -1;
}
return function (a, b){
// a should come before b in the sorted order
if(a[property] < b[property]){
return -1 * sort_order;
// a should come after b in the sorted order
}else if(a[property] > b[property]){
return 1 * sort_order;
// a and b are the same
}else{
return 0 * sort_order;
}
}
}
let employees = [
{
firstName: 'John',
age: 27,
joinedDate: 'December 15, 2017'
},
{
firstName: 'Ana',
lastName: 'Rosy',
age: 25,
joinedDate: 'January 15, 2019'
},
{
firstName: 'Zion',
lastName: 'Albert',
age: 30,
joinedDate: 'February 15, 2011'
},
{
firstName: 'ben',
lastName: 'Doe',
joinedDate: 'December 15, 2017'
},
{
firstName: 'Tom',
lastName: 'Doe',
joinedDate: 'December 15, 2017'
},
];
console.log("Object to be sorted");
console.log(employees);
console.log("Sorting based on the age property")
console.log(employees.sort(dynamicsort("age","desc")));
console.log("Sorting based on the age property")
console.log(employees.sort(dynamicsort("age","asc")));
console.log("Sorting based on the lastName property")
console.log(employees.sort(dynamicsort("lastName","desc")));
console.log("Sorting based on the lastName property")
console.log(employees.sort(dynamicsort("lastName","asc")));
解决方案
您可以提前检查并将值移动undefined
到null
顶部。
function dynamicsort(property, order) {
const
isNullOrUndefined = v => v === undefined || v === null,
sort_order = -(order === "desc") || 1;
return function(a, b) {
return sort_order * (
isNullOrUndefined(a[property]) - isNullOrUndefined(b[property])||
(a[property] > b[property]) - (a[property] < b[property])
);
}
}
const
employees = [{ firstName: 'John', age: 27, joinedDate: 'December 15, 2017' }, { firstName: 'Ana', lastName: 'Rosy', age: 25, joinedDate: 'January 15, 2019' }, { firstName: 'Zion', lastName: 'Albert', age: 30, joinedDate: 'February 15, 2011' }, { firstName: 'ben', lastName: 'Doe', joinedDate: 'December 15, 2017' }, { firstName: 'Tom', lastName: 'Doe', joinedDate: 'December 15, 2017', age: null }];
console.log("Object to be sorted");
console.log(employees);
console.log("Sorting based on the age property desc")
console.log(employees.sort(dynamicsort("age", "desc")));
console.log("Sorting based on the age property asc")
console.log(employees.sort(dynamicsort("age", "asc")));
console.log("Sorting based on the lastName property desc")
console.log(employees.sort(dynamicsort("lastName", "desc")));
console.log("Sorting based on the lastName property asc")
console.log(employees.sort(dynamicsort("lastName", "asc")));
.as-console-wrapper { max-height: 100% !important; top: 0; }
推荐阅读
- javascript - Webdriver io,文件上传使用输入类型=“文件”
- abap - ASSIGN 因调试器路径中的变量而失败
- android - 使用 ARM ABI 模拟 Android 1.6 Donut
- performance - 定义所有新语法会损害 Scheme、Racket 的性能吗?
- c# - 如何从 appsettings.json 读取 AWS RegionEndpoint?
- android - 为什么使用 NDK 不能存在不同的 stl 实现?
- swift - 如何在swift中制作带有背景颜色的交叉标签?
- sql - 有没有什么可能的方法可以只做大写的“元音”表单列名=“last_name”?
- python - 执行函数时无法从 AWS 层切换到指定本地构建的可编辑 pip3 包
- angular - 如何控制角垫图标的方向