If you are retrieving a subset of all the database data, for example displaying 20 rows on screen out of 1000, it is better to sort on the database. This will be faster and easier and will allow you to retrieve one page of rows (20, 50, 100) at a time instead of all of them.
If your dataset is fairly small, sorting in your code may be more convenient if you want implement a complex sort. Usually this complex sort can be done in SQL
but not as easily as in code.
The short of it is, the rule of thumb is sort via SQL
, with some edge cases to the rule.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…