Where do I sort?

I have a database, which I query, and I’m unsure of where to perform the sorting of the results, so far I’ve have the following options.

  • At the MySQL query.
  • At list level(Using a LinkedList)
  • Sorting an unsorted list using comparators before showing the results (basically in the jsp)

The List is composed by ObjectDTO so where would it be more efficient. Any ideas?

How do I do stable sort?

How do I stably sort an array? The value I want to sort by can have a lot of duplicates, and I’m not sure which sort algorithm ruby uses. I’m thinking insertion sort would have worked best for me. Ex



How do i sort objects?

I’ve created a class and created an array of objects under that class and filled it all up with data. Now i want to sort the entire array by a specific member of that class, how do I do this using the

In wordpress I need to do a sort then a where clause… but I can’t get it working

I have a list of post that need to be in chronological order. The fields are all custom and I am using the Advance custom fields plugin but that shouldn’t matter. What I am trying to do is sort items

How Do I Sort IList?

There’s no Sort() function for IList. Can someoene help me with this? I want to sort my own IList. Suppose this is my IList: public class MyObject() { public int number { get; set; } public string mar

How do I add Sort by options?

I have a ‘Sort By’ dropdown on an events page where users can view a number of events and I’d like to allow users to Sort the events by Name (Alphebetical), Date (Created_At), and perhaps (Number of p

Database: how do I sort GUID?

My primary key uses guid. How do I sort GUID? What about I create a datetime column and record a datetime stamp, I could then sort by datetime? is this the best way to do it? or are there better ways?

How do I sort the following list

I have a list called clusters and in that list there is another list called tags which has a sequenceno. How do I sort clusters by using the max of seuqenceno from tags of each cluster using lambda ex

In ruby/rails, how do I sort on a date value where the date can sometimes be null?

I would like to sort my games by game_date, but sometimes the game_date may be null, and I will get an exception: undefined method `to_datetime’ for nil:NilClass @games = @teams.reduce([]) { |memo, te

Where can I get C/C++ sample code for merge sort a link list?

Where can I get a sample code for merge sort a link list?

How do I sort a QList of QDateTime*?

How do I sort a QList of QDateTime* objects by the value of the QDateTime object?

Answers

Database. Using indexes and other information about the data, db’s are very good at this.

You should do the sorting in the database if at all possible.

  • The database can use indexes. If there is a suitable index available then the results can be read from disk already in sorted order, resulting in a performance increase – no extra O(n log(n)) sorting step is required.
  • If you only need the first x results you also minimize data transfer (both reduced network transfer, and also reduced disk access if there is a suitable index).

Best is at the mySQL query.

a) It is easy to do

b) If you use an index the sort happens when the index is created or when new rows are inserted automatically (sometimes an index needs a reorganization but this is a db admins daily business. This applies if the table is very huge.).

e) If the index includes the columns used in the where clause the access in general is faster

d) You do not need to read the whole table each time to do the sort for yourself

e) Even if you have no index I believe the DB can do the sorting best

Hope it helps