All is going well using orderBy method when sorting multiple fields on an HTML table. Score A field will be in descending order while Score B field will be in an ascending order. Let's say we have this table with the following data:
Name Score A Score B
A 3
B 7
C 8
D 1
E 2
Using orderBy method:
_.orderBy(scores, ['score_a', 'score_b'], ['desc', 'asc']);
The display would be:
Name Score A Score B
E 2
B 7
C 8
A 3
D 1
However, I want to prioritize the display of a particular field and wanted Score A field to be displayed first instead of Score B. Desired display would be:
Name Score A Score B
C 8
A 3
D 1
E 2
B 7
How to achieve this? Tried also chaining lodash methods. Is this something that should be adjusted on the display? Need your awesome inputs. Thanks.
Interchanging 'a' and 'b' score may solve the issue
var scores = [{
'score': 'a',
'a': 3,
'b': 0
},
{
'score': 'b',
'a': 0,
'b': 7
},
{
'score': 'c',
'a': 8,
'b': 0
},
{
'score': 'd',
'a': 1,
'b': 0
},
{
'score': 'e',
'a': 0,
'b': 2
}
];
// Sort by `user` in ascending order and by `age` in descending order.
console.log(_.orderBy(scores, ['b', 'a'], ['asc', 'desc']));
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>