Sorting a list of records alphabetically: is there any reason NOT to let the database do it I have a list of records that contain a column of arbitrary strings. I would like the application to display them sorted in some order (preferably alphabetically, since humans have to read the data).

I can choose to have the database sort the data before returning it to me, or I can choose to have the database give me the raw search results and let the application deal with it.

Currently the database is sorting it cause I don't think I can write a very efficient sort anyways, but I'm wondering whether there are any situations where I want a sorted list, but it would be better to have the application do it?
This is a way to broad question to give a detailed answer. However, there is one general rule that holds true in development:


  Don't reinvent the wheel


That is, unless you have a good reason. In the case of sorting, the developers of the database engine spend a lot more time writing an efficient sorting algorithm than you will be able to. So unless you need to sort by something like a field that is encrypted and cannot be decrypted on the database server, I would always sort on the database side.

However, if you have changing sorting requirements, like a display were the user selects which column to sort by, you are better of getting the data once and then sorting it in memory. The alternative of transferring the data again and again every time the sort order changes might be just too chatty on the network. Also a fully flexible query tends to be hard to optimize by the rdbms.

But then, if you are returning only the top few rows, sorting on the database site becomes a lot faster again, as the amount of data going over the network is greatly reduced.

That really brings us to the second general rule in database development:


  It depends.


It hope this was at least a little helpful. For a more detailed answer please give us more detail on your actual problem.