Array sorting in Front-end or Back-end

I would actually try to hand it off to the database. Write the query where the array returned from the DB is already sorted. The most computing power and most efficient sorting is probably on the DB.

This goes to sorting, filtering in general IMO.


Depends on from where data come, how much of it do you have, and what goals you want to achieve.

Frontend based solution is cost less server CPU but could become terrible user experience. Imagine an array of 100000 features sorted in IE installed on old PC? It could hang the browser.

So if you have hot much data to process or CPU economy on server is important for you use frontend, otherwise backend.


Your API will be used by n clients. Performance-wise it would make sense to have each client do the sorting on their own instead of having the server do it for all the n clients. Simply, less CPU work for the server.

Furthermore, whether the result needs to be sorted or not depends on the nature of the application using the data. Let the application decide that. Some interfaces allow the user to decide what to sort by, thereby convenient to do it locally (without waiting for a background HTTP call).

However I would not overthink the performance part before you actually have a performance problem. It could also be that the data sorting is not really costly or the sorting has already been done depending on how information is kept internally (in DBMS-s, for example).

Edit

With up to 20 rows without sorting, it really makes no important difference - make the API implementing developers' life easier and do the small sorting on the frontend side.