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

220
Views
Data structure problem related to items filtering

I have this data:

enter image description here

There are three columns r_code, wtg_code and rr. Every row can be treated as a collection of object properties. We have to select n number of rows from the existing rows. Considering the row are sorted in decreasing order of r_code, constraints are,

  1. Select the rows in decreasing order of r_code, many rows can have the same r_code.
  2. If many rows have the same r_code, then select on the basis of decreasing wtg_code.
  3. If multiple rows have the same wtg_code then select on the basis of lowest rr.

If you can give the solution in Javascript then it will be better otherwise I welcome any language.

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

0

In JavaScript, the data can be represented like this:

let data = [
    { r_code: 5, wtg_code: 8, rr: 3.4 },
    { r_code: 5, wtg_code: 8, rr: 3.4 },
    { r_code: 5, wtg_code: 7, rr: 4.5 },
    { r_code: 4, wtg_code: 6, rr: 1.2 },
    { r_code: 4, wtg_code: 6, rr: 2.4 },
    { r_code: 4, wtg_code: 6, rr: 4.5 },
    { r_code: 2, wtg_code: 6, rr: 1.6 },
    { r_code: 2, wtg_code: 5, rr: 7.4 },
    { r_code: 1, wtg_code: 4, rr: 3.1 },
    { r_code: 1, wtg_code: 4, rr: 2.9 },
    { r_code: 1, wtg_code: 3, rr: 3.3 },
];

Considering the row are sorted in decreasing order of r_code

That could still allow for several permutations. The rules for selection really mean that you should sort the data more precisely: by descending r_code, descending wtg_code and ascending wtg_code.

For that you can use the sort method.

data.sort((a, b) => b.r_code - a.r_code || b.wtg_code - a.wtg_code || a.rr - b.rr);

Finally, if you are interested in the first 10 (n=10), then slice:

let n = 10;
console.log(data.slice(0, n));
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!