I am having problems sorting array of objects of this format
const arr = [
{ name: "test_1", age: 15 },
{ name: "test_11", age: 20 },
{ name: "test_12", age: 34 },
{ name: "test_2", age: 35 },
{ name: "test", age: 39 },
{ name: "example", age: 35 },
];
With the code that I have tried, it sorts based on string, hence I am getting wrong output. Can someone help me out with this using lodash
import orderby from "lodash.orderby";
const arr = [
{ name: "test_1", age: 15 },
{ name: "test_11", age: 20 },
{ name: "test_12", age: 34 },
{ name: "test_2", age: 35 }
];
function sortArr(field, order) {
const result = orderby(
arr,
[
(item) =>
typeof item[field] === "string"
? item[field].toLocaleLowerCase()
: item[field]
],
order
);
return result;
}
console.log(sortArr("name", "asc"));
Output that I am expecting, test_1 test_2 test_11 test_12
Help wil be appreciated