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

216
Visualizações
Better aproach for sorting numerical and text string?

I have a table with column names inside <th> and only numbers inside <td>. When I click on the <th> the sorting algoritm sorts values asc or desc.

.sortElements(function (a, b) {
    return $.text([a]) > $.text([b]) ?
          inverse ? -1 : 1
          : inverse ? 1 : -1;
}

This sorts the text but there is the eternal string vs number problem. This sorts as string so 10 is smaller than 2 so I did this

.sortElements(function (a, b) {
    return parseInt($.text([a]),10) > parseInt($.text([b]),10) ?
          inverse ? -1 : 1
          : inverse ? 1 : -1;
}

This is sorting in the correct way (correct for my needs) if you have only numbers and 10 is bigger than 2. Now I ask you... Is there a better way to do this?

What about if you have also letters inside <td>? What must be done? This is for someone who needs this to work correctly no matter what is inside <td>...?

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

0

You can use the Number() constructor for better readability

.sortElements(function (a, b) {
    return Number($.text([a])) > Number($.text([b])) ?
          inverse ? -1 : 1
          : inverse ? 1 : -1;
}
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