使用_.sortByOrder lodash按数字字符串排序

时间:2017-01-12 07:12:54

标签: javascript lodash

我正在尝试使用sortbyorder lodash函数对数字字符串进行排序。

函数应该在仅字符串字段上正常工作,但如果是数字字符串,则应按数字顺序排序

示例数组对象如下:

[{
  "timeInProcessing": "20 min",
  "timeInManual": "8 min",
  "taskID": "653452",
  "reasonType": "Customer Request",
  "assignedStatus": "Robinson, Edwin",
  "virtualMachine": "[machine name]",
  "lastAction": "1st processing fail",
  "region": "EU",
  "project": "Demo Chue STAGE Media Extracts 04",
  "fileName": "Depósito à Prazo BC - BI de Abril a 08 JUN 2016.xlsx",
  "index": "1.0",
  "fileRoom": "NRP TriPost",
  "fileType": "xlsx",
  "fileSize": "22.49 MB",
  "processedBy": "n/a",
  "uploadedBy": "Johnson III, Chadwick",
  "node": "SPWD6PDGDS001"
}, {
  "timeInProcessing": "15 min",
  "timeInManual": "7 min",
  "taskID": "765435",
  "reasonType": "Multiple Attachments",
  "assignedStatus": "Robinson, Edwin",
  "virtualMachine": "[machine name]",
  "lastAction": "2nd processing fail",
  "region": "EU",
  "project": "Blue Thunder",
  "fileName": "lorem_ipsum_dolor.msg",
  "index": "1.1",
  "fileRoom": "North America",
  "fileType": "msg",
  "fileSize": "0.51 MB",
  "processedBy": "Chandwik, Eric",
  "uploadedBy": "Williamson, Lucinda",
  "node": "SPWD6PDGDS002"
}, {
  "timeInProcessing": "10 min",
  "timeInManual": "n/a",
  "taskID": "765436",
  "reasonType": "Customer Request",
  "assignedStatus": "Unassigned",
  "virtualMachine": "n/a",
  "lastAction": "[TBD]",
  "region": "AP",
  "project": "Hercules",
  "fileName": "lorem_ipsum_dolor.msg",
  "index": "1.1.1",
  "fileRoom": "STAGING-Enterprise HR",
  "fileType": "msg",
  "fileSize": "0.01 MB",
  "processedBy": "Holland, Roberta",
  "uploadedBy": "Trisko, Dora",
  "node": "SPWD6PDGDS005"
}, ]

我要对其进行排序的字段为timeInProcessing

2 个答案:

答案 0 :(得分:3)

您可以使用排序回调。它接受一个键并返回数值或原始值,如果值为NaN

function byKey(key) {
    return function (o) {
        var v = parseInt(o[key], 10);
        return isNaN(v) ? o[key] : v;
    };
}

使用lodash 4.17.2 _.sortBy

function byKey(key) {
    return function (o) {
        var v = parseInt(o[key], 10);
        return isNaN(v) ? o[key] : v;
    };
}

var data = [{ timeInProcessing: "20 min", timeInManual: "8 min", taskID: "653452" }, { timeInProcessing: "15 min", timeInManual: "7 min", taskID: "765435" }, { timeInProcessing: "10 min", timeInManual: "n/a", "taskID": "765436" }, { timeInProcessing: "min", timeInManual: "n/a", "taskID": "7654XX" }, { timeInProcessing: "abc", timeInManual: "n/a", "taskID": "7654YY" }],
    sorted = _.sortBy(data, byKey('timeInProcessing'));

console.log(sorted);
_.reverse(sorted);
console.log(sorted);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>

使用lodash 3.10.1 _.sortByOrder

function byKey(key) {
    return function (o) {
        var v = parseInt(o[key], 10);
        return isNaN(v) ? o[key] : v;
    };
}

var data = [{ timeInProcessing: "20 min", timeInManual: "8 min", taskID: "653452" }, { timeInProcessing: "15 min", timeInManual: "7 min", taskID: "765435" }, { timeInProcessing: "10 min", timeInManual: "n/a", "taskID": "765436" }, { timeInProcessing: "min", timeInManual: "n/a", "taskID": "7654XX" }, { timeInProcessing: "abc", timeInManual: "n/a", "taskID": "7654YY" }],
    sorted = _.sortByOrder(data, byKey('timeInProcessing'), ['asc']);

console.log(sorted);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.min.js"></script>

答案 1 :(得分:0)

我在您的对象sortKey中添加了一个属性。这是为timeInProcessing保存数值。这将允许您使用数值进行排序。

同样在以下示例中,我首先使用uploadedBy进行排序,然后使用解析的值来描述对多于一个键的排序

var data=[{timeInProcessing:"20 min",timeInManual:"8 min",taskID:"653452",reasonType:"Customer Request",assignedStatus:"Robinson, Edwin",virtualMachine:"[machine name]",lastAction:"1st processing fail",region:"EU",project:"Demo Chue STAGE Media Extracts 04",fileName:"Depósito à Prazo BC - BI de Abril a 08 JUN 2016.xlsx",index:"1.0",fileRoom:"NRP TriPost",fileType:"xlsx",fileSize:"22.49 MB",processedBy:"n/a",uploadedBy:"Johnson III, Chadwick",node:"SPWD6PDGDS001"},{timeInProcessing:"15 min",timeInManual:"7 min",taskID:"765435",reasonType:"Multiple Attachments",assignedStatus:"Robinson, Edwin",virtualMachine:"[machine name]",lastAction:"2nd processing fail",region:"EU",project:"Blue Thunder",fileName:"lorem_ipsum_dolor.msg",index:"1.1",fileRoom:"North America",fileType:"msg",fileSize:"0.51 MB",processedBy:"Chandwik, Eric",uploadedBy:"Williamson, Lucinda",node:"SPWD6PDGDS002"},{timeInProcessing:"10 min",timeInManual:"n/a",taskID:"765436",reasonType:"Customer Request",assignedStatus:"Unassigned",virtualMachine:"n/a",lastAction:"[TBD]",region:"AP",project:"Hercules",fileName:"lorem_ipsum_dolor.msg",index:"1.1.1",fileRoom:"STAGING-Enterprise HR",fileType:"msg",fileSize:"0.01 MB",processedBy:"Holland, Roberta",uploadedBy:"Trisko, Dora",node:"SPWD6PDGDS005"}];

var sortedOrder = _.sortByOrder(_.map(data, function(o) {
  o["timeInProcessing_num"] = parseInt(o.timeInProcessing, 10);
  return o;
}), ["uploadedBy", "timeInProcessing_num"])
console.log(sortedOrder)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.0/lodash.min.js"></script>