首页 > 解决方案 > 对象的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")));

标签: javascriptsortingjavascript-objects

解决方案


您可以提前检查并将值移动undefinednull顶部。

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


推荐阅读