javascript - 发送列索引到排序函数
问题描述
这是一个按第一列对二维数组进行排序的函数。
var a = [[12, 'AAA'], [58, 'BBB'], [28, 'CCC'],[18, 'DDD']];
console.log(a.sort(sortFunction,100));
function sortFunction(a, b) {
if (a[0] === b[0]) {
return c;
}
else {
return (a[0] < b[0]) ? -1 : 1;
}
}
如何将列索引发送到函数而不是硬编码 0?
解决方案
也许你可以尝试使用柯里化:
sortFunction = index => (a, b) => {
if (a[index] === b[index]) {
return 0;
}
else {
return (a[index] < b[index]) ? -1 : 1;
}
}
所以用法会像
console.log(a.sort(sortFunction(1)));
推荐阅读
- laravel - 如何限制两个日期之间的记录显示
- excel - 使用 VBA 打开文件夹并按修改日期排序?
- kotlin - 使用反射在Kotlin中按字符串设置属性?
- botframework - Microsoft Teams Bot - 用户无法发起对话
- sap-cloud-sdk - 对函数导入的 VDM 调用返回 null 而不会引发异常
- c# - 如何在 C# 中使用 geckofx 浏览器控件中的代码触发 LauncherDialog.Download 事件?
- reactjs - 无法清除超时 React 功能组件
- r - 使用 R 在 Elasticsearch 中索引数据时出错
- ios - 如何在没有越狱设备的情况下测试越狱措施是否有效
- java - 在调度程序中捕获 Throwable 是一种好习惯吗?