Thursday, September 08, 2005

Where do I sort?

Sometimes there are questions about where sorting should occur.

1) Should I sort in the database?
2) Should I sort in the business layer?
3) Should I sort in the display layer?

The time you need to sort - is when you need a sorted collection. You cannot rely on someone else having given you a collection in the correct order, thsi tends to make thing hard to understand and refactor.

For example, if you sort in the database - how does your display layer have any knowledge of how things are sorted? How does it know the sort order won't change? The safest thing to do is sort at the point where it is obvious you need to sort. Sometimes that will be in the business logic, other times in teh database, and other times in the display layer.

But, you say, I might create a performance problem if I sort too often!

Wait until it's a problem - it probably won't matter.


Post a Comment

<< Home