Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

221
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda