For some reason, I'm having trouble sorting my data using lodash in my front-end client code.
All the examples I've come across don't involve working with data in an interface, so I can't figure out where I'm going wrong.
Let's consider a scenario where I need to sort products by isInPriceList (descending), listorder (descending), and name (ascending).
You can check out my stackblitz example at https://stackblitz.com/edit/typescript-lodash-playground-kbtjbg
interface IProduct {
name: string;
isInPriceList: boolean;
listOrder: number;
}
...add some data
const sortedData = _.orderBy( data, p => [ p.isInPriceList, p.listOrder, p.name ], [ "desc", "desc", "asc"]);
Unfortunately, the above code doesn't sort the data as intended. However, the following approach does work:
let correctlySortedData = _.orderBy( data, p => p.name, "asc");
correctlySortedData = _.orderBy( correctlySortedData, p => p.listOrder, "desc" );
correctlySortedData = _.orderBy( correctlySortedData, p => p.isInPriceList, "desc" );
I suspect there may be an issue with the second parameter being passed.