按字母数字排序对象数组

时间:2019-03-14 14:38:34

标签: javascript sorting lodash alphanumeric

我有一个对象数组,需要按字母数字顺序对其进行排序。我可以使用下面的代码轻松地对一个简单的字符串/数字数组进行字母数字排序。但是我需要以相同的方式对对象数组进行排序。我曾尝试编写自己的排序函数,但似乎无法按字母数字顺序对一组对象进行正确排序。下面的示例是我的对象数组的外观,我需要按“ ClientID”对该对象数组进行排序。 正确排序后,前三个ClientID应该为21、41、45等。

/** Array of Objects **/
    var array = [
    { Code: "Code1S3", ClientID: "211", ReceiptDate: "2018-10-19T04:00:00.000Z" },
    { Code: "Code1S43", ClientID: "2247", ReceiptDate: "2019-01-08T05:00:00.000Z" },
    { Code: "Code1S12", ClientID: "3678", ReceiptDate: "2018-10-19T04:00:00.000Z" },
    { Code: "Code1S10", ClientID: "39920", ReceiptDate: "2018-10-19T04:00:00.000Z" },
    { Code: "Code1S19", ClientID: "39920", ReceiptDate: "2018-11-19T05:00:00.000Z" },
    { Code: "Code1S4", ClientID: "21", ReceiptDate: "2018-10-19T04:00:00.000Z" },
    { Code: "Code1S35", ClientID: "338x24", ReceiptDate: "2018-12-18T05:00:00.000Z" },
    { Code: "Code1S13", ClientID: "40466", ReceiptDate: "2018-10-19T04:00:00.000Z" },
    { Code: "Code1S22", ClientID: "40466", ReceiptDate: "2018-11-19T05:00:00.000Z" },
    { Code: "Code1S9", ClientID: "40656", ReceiptDate: "2018-10-19T04:00:00.000Z" },
    { Code: "Code1S18", ClientID: "40656", ReceiptDate: "2018-11-19T05:00:00.000Z" },
    { Code: "Code1S5", ClientID: "41", ReceiptDate: "2018-10-19T04:00:00.000Z" },
    { Code: "Code1S24", ClientID: "2468", ReceiptDate: "2018-11-19T05:00:00.000Z" },
    { Code: "Code1S11", ClientID: "41193", ReceiptDate: "2018-10-19T04:00:00.000Z" },
    { Code: "Code1S20", ClientID: "41193", ReceiptDate: "2018-11-19T05:00:00.000Z" },
    { Code: "Code1S21", ClientID: "36780", ReceiptDate: "2018-11-19T05:00:00.000Z" },
    { Code: "Code1S36", ClientID: "45", ReceiptDate: "2019-01-08T05:00:00.000Z" },
    { Code: "Code1S31", ClientID: "52x386", ReceiptDate: "2018-11-19T05:00:00.000Z" },
    { Code: "Code1S1", ClientID: "550", ReceiptDate: "2018-10-19T04:00:00.000Z" },
    { Code: "Code1S23", ClientID: "41318", ReceiptDate: "2018-11-19T05:00:00.000Z" },
    { Code: "Code1S2", ClientID: "526", ReceiptDate: "2018-10-19T04:00:00.000Z" },
    { Code: "Code1S14", ClientID: "41318", ReceiptDate: "2018-10-19T04:00:00.000Z" },
    { Code: "Code1S32", ClientID: "63x386", ReceiptDate: "2018-11-19T05:00:00.000Z" },
    { Code: "Code1S30", ClientID: "68x34", ReceiptDate: "2018-11-19T05:00:00.000Z" }
    ];

/** Example of Alphanumeric Sort on Array of Strings **/
var data = [];
_.each(array, function(value) {
  data.push(value.ClientID);
});

var collator = new Intl.Collator(undefined, {
  numeric: true,
  sensitivity: 'base'
});
console.log("sorted: ", data.sort(collator.compare));

/** Sorting attempts, ( they don't sort alphanumerically ) **/
var sorted = _.sortBy(array, 'ClientID');
console.log("sorted: ", sorted);

function compare(a, b) {
  if (a.ClientID < b.ClientID)
    return -1;
  if (a.ClientID > b.ClientID)
    return 1;
  return 0;
}

array.sort(compare);
console.log(array)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

更新(已解决): 我解决了这个问题,首先遍历了数组,并得到了要排序的字符串数组。我按字母数字顺序对该数组进行了排序,并保存了已排序索引的另一个数组。我使用该索引数组将对象的初始数组排序为正确的顺序。下面的代码...

function sortWithIndeces(toSort) {
    var collator = new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' });
    for (var i = 0; i < toSort.length; i++) {
        toSort[i] = [toSort[i], i];
    }
    toSort.sort(collator.compare);
    toSort.sortIndices = [];
    for (var j = 0; j < toSort.length; j++) {
        toSort.sortIndices.push(toSort[j][1]);
        toSort[j] = toSort[j][0];
    }
    return toSort;
}
function reorderByIndexes(arr, order) {
    return order.map((index) => arr[index]);
}

// Success
var data = [];
_.each( array, function(value) {
    data.push( value.ClientID );
});

var sortedArray = sortWithIndeces( data )
var sortedIndices = sortedArray.sortIndices;
var Sorted = reorderByIndexes( array, sortedIndices);
console.log("sorted: ", Sorted );

0 个答案:

没有答案