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

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

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 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