I am using lodash.orderBy method and try to sort data like this:
I have two external sort conditions: condition1 and condition2
const condition1 = 'aa';
const condition2 = 'b';
const datas = [
{ f1: 'aaa', f2: 'b', f3: '2019-01-01' },
{ f1: 'a', f2: 'b', f3: '2019-01-02' },
{ f1: 'a', f2: 'c', f3: '2019-01-08' },
{ f1: 'aa', f2: 'b', f3: '2019-01-08' },
{ f1: 'aa', f2: 'b', f3: '2019-01-09' },
{ f1: 'aa', f2: 'c', f3: '2019-01-07' },
{ f1: 'xxx', f2: 'c', f3: '2019-01-03' },
{ f1: 't', f2: 'd', f3: '2019-01-04' },
{ f1: 'a', f2: 'd', f3: '2019-01-01' },
{ f1: 'aa', f2: 'd', f3: '2019-01-02' },
];
I expect data sorted like this:
const expect = [
{ f1: 'aa', f2: 'b', f3: '2019-01-09' },
{ f1: 'aa', f2: 'b', f3: '2019-01-08' },
{ f1: 'aa', f2: 'c', f3: '2019-01-07' },
{ f1: 'aa', f2: 'd', f3: '2019-01-02' },
{ f1: 'a', f2: 'b', f3: '2019-01-02' },
{ f1: 'aaa', f2: 'b', f3: '2019-01-01' },
{ f1: 'a', f2: 'c', f3: '2019-01-08' },
{ f1: 't', f2: 'd', f3: '2019-01-04' },
{ f1: 'xxx', f2: 'c', f3: '2019-01-03' },
{ f1: 'a', f2: 'd', f3: '2019-01-01' },
];
The sorting rule is:
- sort by
f1if the value off1equal withcondition1- priority 1 - sort by
f2iff1is same, the value off2should equal withcondition2- priority 2 - sort by
f3(desc) whenf1andf2are same - priority 3
priority 1 is the highest.
Can't figure it out. Thanks for your help. lodash.orderBy and sort method of javascript, both of these solutions are ok.