views:

58

answers:

1

I have a table with a list of user names, how do I implement a custom sort order for the following scenarios.

create table User (name varchar(255))

e.g. User {John, Sam, Mike} the order could be re-arranged in the UI using the Up / Down buttons to {Sam, John, Mike} or {Sam, Mike, John}

Do I need to add a separate column to the table (e.g. sortOrder - integer data type) to take care of this sorting.

So that, I can set the sortOrder information on all rows every time there is a modification in the UI. The drawback with this mechanism is that every time there is a small modification, the sortOrder column needs to be re-written for the affected rows.

We also tried setting modified_date (date type) as an alternative to the sortOrder column.

Is there a better way to acheive this? Appreciate any help on this topic

+2  A: 

Yes, I think adding a "SortOrder" column is probably the most efficient and most flexible solution to your challenge here. Anything else will always depend on some other sort criteria - alphabetically or some e.g. date or something, but won't give you the ability to get any arbitrary sorting done, really.

Use a SortOrder column! It'll work perfectly fine for most cases!

marc_s
The above suggestion partially solves my problem. a. I am having trouble identifying the list of rows to be modified if we move certain items up or down in a single operation.b. If you add a new entry an appropriate sortOrder value needs to be generated for the new entry. You need the highest sortOrder value in the existing table to acheive this.These become more problematic if the table is quite huge. Is there a better way to implement the above requirement.
Joshua