3

In a specific table I have a SortOrder integer field that tells my page in which order to display the data. There are sets of data in the this field (based on a CategoryID field), and each set will have its own ordering. Users can add/remove/update records in this table.

My question is what is the best way to manage this SortOrder field? I would like to "reseed" it everytime a record is deleted or updated. Is this something I should be using a trigger for? Or should my code handle it and manage the reseeding?

4
  • why would you need to "reseed" after a delete? Why does a potential gap in the sortorder numbers bother you? Commented Nov 23, 2009 at 21:51
  • 1
    @marc_s: It offends the OPs OCD =) Commented Nov 23, 2009 at 22:03
  • It makes it easier to also reorder the items. Moving up is simply +1 and moving down is -1. Commented Nov 23, 2009 at 22:07
  • @marc_s: I agree 100% with the OP, gaps leads to mess and is error-prone and bad behavior, SortOrder is about order, I don't think it's about OCD, I think it's about natural ordered order }:) it has to be consistent. Commented Feb 11, 2011 at 7:02

2 Answers 2

2

What I used to do is use only odd numbers in the SortOrder field so upon changing the order, I would add or subtract 3 from the current value of the modified item and then do a reseed (order the items again using odd number indexes). Also I used to reseed after every insert or delete.

Sign up to request clarification or add additional context in comments.

Comments

1

All you really have to worry about is swapping any two fields. All new entries go to the end and i'm sure you've got a mechanism by which the user can change the order. The order change, move up or down, really is a swap with a neighboring field. All you really care about is that all the fields are sorted properly. Don't let a mathematical sense of aesthetic drive you into creating something overly complex. (You'll end up with holes in your sequence after deletes are made but that's OK. It's an internal sequence marker used for ORDER BY. the numbers don't need to be made contiguous.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.