sort array of numeric strings with Lodash
sortBy
can be used with a function instead of a property name.
_.sortBy(arrData, function (obj) {
return parseInt(obj.rhid, 10);
});
This can be achieved using arrow notation like:
_.sortBy(arrData, (obj) => parseInt(obj.val, 10));
If you want to do more than one field like @GunasekaranR asked, you can also do it with arrow notation:
_.sortBy(arrData, [
(obj) => parseInt(obj.first_val, 10),
(obj) => parseInt(obj.second_val, 10)
]);
The second way uses first_val
as the main sorting object, with second_val
being the tie breaker.