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

111
Vistas
Sort array of 50 000 objects - Javascript

I need to sort an array of objects in javascript(angular project).

My object is fairly simple, just a single layer of some strings and ints:

{
    id: 7683,
    base: "example",
    value: 1000000,
    poster: "example",
    price: 100,
    itemCount: 49
}

There are going to be 50 000 of these objects, and I'm displaying them in a table, where the user should be able to sort by any of the properties (I'm paginating them to save resources during rendering).

My current implementation is the base javascript sort alg with a custom sort func:

this.myList.sort((a,b) => a.id - b.id)

Currently this takes around 25 seconds, which is not the user experience I'm after. I tested a datatable implementation, which seemed to work quickly, so I'm trying to find out why mine is not as fast.

What is the fastest way to accomplish my sort? Do I need to write a custom quick sort? If so, do you mind explaining what about the default sort with a custom function makes it slower than something I might write myself? Thanks

Edit:

Thanks to those who highlighted that it should not be taking this long. I was not sure of a reasonable timeframe for a sort like this. After creating some tests cases it seems it was the painting/rendering of the UI elements that was taking the time(despite chrome debugger saying it was all scripting, maybe my understanding of this is incorrect). Thanks for your help.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

I recommend to you using mat-sort its very fast.

Another things you can do to improve sorting:

  1. If you can - sorting by server is way more faster and easier.
  2. Check how long it takes to the API request to fully get the data - "25 seconds" may cause by slow network / file format (like string that need to be parsed / gzip and more)... and not the sort itself.
  3. Render the data only after the data is already sorted (like sorting in rxjs pipe / *ngIf...).
  4. You can use trackBy it can speed the process by a little.
  5. You can save the data of the first paginator page in localStorage - then in the second time you can render to the user the first page (from localStorage) - while you sorting "behind the curtain" and rerendering the full data.
  6. There is a few more solution it depend on the app you've got - for example if its a small app with related data - you can sort the data during app initialization APP_INITIALIZER
  7. Angular material table/paginator/sort can speed up the ordering a lot - this link example is examine 23,575 objects.
about 4 years ago · Juan Pablo Isaza Denunciar

0

try some solution like quicksort Algorithm: https://github.com/duereg/js-algorithms/blob/master/lib/algorithms/11-sorting/quickSort.js

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