js sort() custom function how can i pass more parameters?

后端 未结 4 1255
醉梦人生
醉梦人生 2020-12-15 10:55

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 att

4条回答
  •  太阳男子
    2020-12-15 11:11

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

提交回复
热议问题