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

146
Visualizações
JavaScript Sort by number in a string

How could i sort e.g an array like this one:

[
  '<@520968882077433856> is level **7,64** _(18115.5 total xp)_',
  '<@289037609274048513> is level **7,07** _(14473 total xp)_',
  '<@672835870080106509> is level **8,25** _(22473.5 total xp)_',
  '<@536686935134175254> is level **6,22** _(10184.5 total xp)_',
]

I would like to sort it by the amount of total xp, so the number within the brackets.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can use the sort callback. Extract the number with a regular expression:

const data = [
  '<@520968882077433856> is level **7,64** _(18115.5 total xp)_',
  '<@289037609274048513> is level **7,07** _(14473 total xp)_',
  '<@672835870080106509> is level **8,25** _(22473.5 total xp)_',
  '<@536686935134175254> is level **6,22** _(10184.5 total xp)_',
];

const regex = /(?<=_\()[\d.]+/;
data.sort((a, b) => +a.match(regex) - +b.match(regex));
console.log(data);

The + is used to convert the returned array (with one match element) to a string and then that to a number. You could use ?.[0] to actually get that first array element.

about 4 years ago · Juan Pablo Isaza Relatório

0

We can try sorting using a custom lambda expression:

var input = [
    '<@520968882077433856> is level **7,64** _(18115.5 total xp)_',
    '<@289037609274048513> is level **7,07** _(14473 total xp)_',
    '<@672835870080106509> is level **8,25** _(22473.5 total xp)_',
    '<@536686935134175254> is level **6,22** _(10184.5 total xp)_',
];
var output = input.sort( (a, b) => parseFloat(a.match(/(\d+(?:\.\d+)?) total xp\b/)[1]) - parseFloat(b.match(/(\d+(?:\.\d+)?) total xp\b/)[1]));
console.log(output);

In the above we are extracting the number which is followed by total xp, parsing it as a float, and then using that value in the sorting comparator. If you want descending order, then just reverse the inequality in the lambda function.

about 4 years ago · Juan Pablo Isaza Relatório

0

Here is another try, looking for a space after the number:

const arr=[
  '<@520968882077433856> is level **7,64** _(18115.5 total xp)_',
  '<@289037609274048513> is level **7,07** _(14473 total xp)_',
  '<@672835870080106509> is level **8,25** _(22473.5 total xp)_',
  '<@536686935134175254> is level **6,22** _(10184.5 total xp)_',
];

function getN(s){
 return s.match(/_\((.+?) /)[1];
}
arr.sort((a,b)=>getN(a)-getN(b))

console.log(arr)

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