I have an array of objects that I need to sort alphanumerically. I can easily sort a simple array of strings/numbers alphanumerically with the code below. But I need to sort an array of objects the same way. I’ve tried writing my own sorting functions but I cannot seem to alphanumerically sort an array of objects properly. The example below is what my array of objects looks like, I need to sort this array of objects by the ‘ClientID’. When sorted properly the first three ClientID’s should be 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>
UPDATE (Solved): I solved this, I first looped through the array and got an array of strings to sort. I alphanumerically sorted that array and saved another array of the sorted indexes. I used that array of indices to sort the initial array of objects to the proper order. The code below...
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 );