Sorting on the Server or on the Client

Should data sorting be done on the client or on the server?

Each approach has its pros and cons:

  • If you need pagination, and don't want to download the entire data to the client, then you must perform the sorting on the server (otherwise the client can only sort the rows it currently has, which will lead to wrong results, if you re-sort by a different column)
  • Sorting on the server is faster (as in: you can sort more rows/second), but if you have to serve 10000 clients at once, this may easily invert.
  • When sorting on the client, you can re-sort without downloading the data again.

Sorting on the server or on the client?

In general, you should let the database do the sorting; if it doesn't have the resources to handle this effectively, you need to upgrade your database server.

First off, the database may already have indexes on the fields you want so it may be trivial for it to retrieve data in sorted order. Secondly, the client can't sort the results until it has all of them; if the server sorts the results, you can process them one row at a time, already sorted. Lastly, the database is probably more powerful than the client machine and can probably perform the sorting more efficiently.

Sorting and Filtering on the client side. How should approach this?

The best way to handle sorting I guess should be to handle it on server-side. If you really want to do it on client-side, you should first show data as they are and then trigger onChage when user change sorting or keyword.

I would recommend you to use lodash library: https://www.npmjs.com/package/lodash for sorting and filtering.

Here are some useful link that will tell you more about server-side and client-side sorting.

https://www.c-sharpcorner.com/forums/difference-between-client-side-paging-and-server-side-paging

Pagination: Server Side or Client Side?

Where data sort should be done ? Server or client?

https://softwareengineering.stackexchange.com/questions/249897/web-app-filtering-information-client-side-vs-server-side

SQL Sorting: server vs. client

Always sort where possible in the SQL server.

The SQL abstracts away the steps of querying the database, sorting, etc. Use as much abstraction as you can - why would you select a bunch of rows, and then write a sorting algorithm when you can do it in the SQL with ORDER BY.

See also Bill Karwin's answer.

Is it better to sort from server and distribute to clients or send unsorted and let clients sort it?

This question will probably get closed down as "off topic". However,

First question: does the server ever need a sorted scoreboard?

If not, why do the work?

If anything, the server will want to index the scoreboard by player ID, which argues for either sorting by ID (to aid binary searches) or using a hashed container (O1 search time).

Furthermore, the ultimate bottleneck is network bandwidth. You'll eventually want to be in a position to send scoreboard deltas rather than state-of-the-world messages.

This further argues for making the clients do the work of resorting.

There is one more philosophical argument:

Is sorting by anything other than a primary key a data concern or a presentation concern? It's a presentation concern.

What does presentation? A client.

QED

Sort on server side (PHP/MySQL) or on client side (js)?

You can sort on client side. But it is not efficient if you have a lot of data. In this case you need to provide some pagination mechanism and it makes no sense to sort paginated results clientside.

The answer from me: use server side sorting if you have A LOT of data (hundreds of records may be an indication, but look what works for you). Otherwise load all the data using api call and sort client side.



Related Topics



Leave a reply



Submit