js sort()自定义函数如何传递更多参数?

时间:2022-07-25 18:00:01

I have an array of objects i need to sort on a custom function, since i want to do this several times on several object attributes i'd like to pass the key name for the attribute dinamically into the custom sort function:

我有一个对象数组,我需要对自定义函数进行排序,因为我想在几个对象属性上多次这样做,我想将属性的键名称传递给自定义排序函数:

function compareOnOneFixedKey(a, b) {
    a = parseInt(a.oneFixedKey)
    b = parseInt(b.oneFixedKey)
    if (a < b) return -1
    if (a > b) return 1
        return 0
}

arrayOfObjects.sort(compareByThisKey)

should became something like:

应该成为:

function compareOnKey(key, a, b) {
    a = parseInt(a[key])
    b = parseInt(b[key])
    if (a < b) return -1
    if (a > b) return 1
        return 0
}
arrayOfObjects.sort(compareOn('myKey'))

Can this be done in a convenient way? thanks.

这可以方便吗?谢谢。

3 个解决方案

#1


10  

You would need to partially apply the function, e.g. using bind:

您需要部分应用该功能,例如使用绑定:

arrayOfObjects.sort(compareOn.bind(null, 'myKey'));

Or you just make compareOn return the actual sort function, parametrized with the arguments of the outer function (as demonstrated by the others).

或者你只是让compareOn返回实际的sort函数,使用外部函数的参数进行参数化(如其他函数所示)。

#2


9  

You may add a wrapper:

你可以添加一个包装器:

function compareOnKey(key) {
    return function(a, b) {
        a = parseInt(a[key], 10);
        b = parseInt(b[key], 10);
        if (a < b) return -1;
        if (a > b) return 1;
        return 0;
    };
}

arrayOfObjects.sort(compareOnKey("myKey"));

#3


4  

Yes, have the comparator returned from a generator which takes a param which is the key you want

是的,让比较器从一个生成器返回,该生成器接受一个你想要的密钥

function compareByProperty(key) {
    return function (a, b) {
        a = parseInt(a[key], 10);
        b = parseInt(b[key], 10);
        if (a < b) return -1;
        if (a > b) return 1;
        return 0;
    };
}
arrayOfObjects.sort(compareByProperty('myKey'));

compareByProperty('myKey') returns the function to do the comparing, which is then passed into .sort

compareByProperty('myKey')返回执行比较的函数,然后传递给.sort

#1


10  

You would need to partially apply the function, e.g. using bind:

您需要部分应用该功能,例如使用绑定:

arrayOfObjects.sort(compareOn.bind(null, 'myKey'));

Or you just make compareOn return the actual sort function, parametrized with the arguments of the outer function (as demonstrated by the others).

或者你只是让compareOn返回实际的sort函数,使用外部函数的参数进行参数化(如其他函数所示)。

#2


9  

You may add a wrapper:

你可以添加一个包装器:

function compareOnKey(key) {
    return function(a, b) {
        a = parseInt(a[key], 10);
        b = parseInt(b[key], 10);
        if (a < b) return -1;
        if (a > b) return 1;
        return 0;
    };
}

arrayOfObjects.sort(compareOnKey("myKey"));

#3


4  

Yes, have the comparator returned from a generator which takes a param which is the key you want

是的,让比较器从一个生成器返回,该生成器接受一个你想要的密钥

function compareByProperty(key) {
    return function (a, b) {
        a = parseInt(a[key], 10);
        b = parseInt(b[key], 10);
        if (a < b) return -1;
        if (a > b) return 1;
        return 0;
    };
}
arrayOfObjects.sort(compareByProperty('myKey'));

compareByProperty('myKey') returns the function to do the comparing, which is then passed into .sort

compareByProperty('myKey')返回执行比较的函数,然后传递给.sort