Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

176
Views
lodash orderBy multiple numeric fields display priority

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.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!