Skip to content

Instantly share code, notes, and snippets.

@itashdv
Created August 27, 2021 12:29
Show Gist options
  • Save itashdv/1269d01d942df4069f4cd5094b9f1cee to your computer and use it in GitHub Desktop.
Save itashdv/1269d01d942df4069f4cd5094b9f1cee to your computer and use it in GitHub Desktop.
Case insensitive sorting with ramda sortWith
import { sortWith, path, split, ascend, descend } from "ramda";
const theList = [
{
name: "C",
purchase: {
period: {
start: "2020-01-26T21: 00: 00Z",
end: "3070-10-27T21: 00: 00Z",
},
},
},
{
name: "A",
purchase: {
period: {
start: "2020-01-08T21: 00: 00Z",
end: "3070-10-27T21: 00: 00Z",
},
},
},
{
name: "b",
purchase: {
period: {
start: "2019-09-30T19: 00: 00Z",
end: "3070-10-27T21: 00: 00Z",
},
},
},
];
const makeSorter = compose(
sortWith,
map(({ order, name }) =>
(order == "desc" ? descend : ascend)(
compose(toLower, path(split(".", name)))
)
)
);
const sorts = [
{ order: "asc", name: "name" },
{ order: "desc", name: "purchase.period.start" },
];
const result = makeSorter(sorts)(theList);
@ibrahimzack
Copy link

hi Aitash im ibrahim from Antalya I need to talk to you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment