Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

231
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda