0

I have a website with over 500 elements (e.g. blog post) to be sorted according to publishing date, rating etc.

I tried to sort the 500 contents with javascript. But it is so slow, the page froze for 3 seconds when sorting and taking a lot of CPU.

I am using bubble sort right now, but I doubt other sort methods will have a huge difference as it's only n^2 vs nlogn.

Is there a better approach to sort by javascript? Or should I do it in backend (i.e. with PHP)? Thanks!

And also, how do good CMS usually do the sorting when there are many elements?

leonylyner
  • 117
  • 2
  • 7
  • The last question is too opinion-based/broad. – user202729 Apr 15 '18 at 13:23
  • ... Just sort. `Array.sort`. Did you know about that? `O_o` – user202729 Apr 15 '18 at 13:26
  • Yes, I know, that's why I added "usually" because I just want to see what people think. How about my question? I know some websites can do sorting for 500+ quickly so it's possible, but how did they do it? – leonylyner Apr 15 '18 at 13:27
  • That being said, "what people think" == "opinion-based". – user202729 Apr 15 '18 at 13:27
  • I know how to sort it, but I cannot sort it quickly. My sort took around 3 seconds, but I saw some websites with similar number of elements only about 0.5 seconds and without lag. Does Array.sort have similar performance with bubble sort? I also wanted to sort the array in multiple different ways, like by their rating and date, so Array.sort might not work. – leonylyner Apr 15 '18 at 13:29
  • Just try it and benchmark it. What's your actual not-too-broad problem? – user202729 Apr 15 '18 at 13:31
  • https://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value-in-javascript – user202729 Apr 15 '18 at 13:32

0 Answers0