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.
I recommend to you using mat-sort its very fast.
Another things you can do to improve sorting:
try some solution like quicksort Algorithm: https://github.com/duereg/js-algorithms/blob/master/lib/algorithms/11-sorting/quickSort.js