This has kind of turned into two questions: one for the database layer and one for the in-memory collection. However, I think you can practically bring it back down to a single question if you let the database layer become your source of truth.
The reason I say this is that with roughly 100 items as the maximum likely number of active items in your list, you can pretty much ignore asymptotic complexity. Performance-wise, the most important thing to focus on when you've got this many items is round-trips across network connections (e.g. to the database).
Here's a fairly simple approach you can use. It's similar to something I've done in the past, with similar requirements. (I can't remember if it's exactly the same or not, but close enough.)
- Use a numeric
Order
column to determine the order of your items within the given list. int
should be just fine.
- When you remove an item, decrement the orders of all items in the same list after that item. This can be done with a single
UPDATE
statement in SQL.
- When you add an item, give it an Order value based on the location it's added at, and increment the orders of all items in the same list after that item (again, with a single Update statement).
- When you move an item to a different location, change its Order and then increment or decrement the Orders of all the items between its starting and ending positions.
- Every time a change is made, re-load the entire list of items, in order, from the database to display to the user.
You may want to use stored procs to do more of this work in individual round-trips. Definitely a transactions to avoid race conditions.
An approach like this will easily scale for individual users editing individual lists. If you need scalability in terms of concurrent users, it's likely that another strategy like a NoSQL store is going to be the way to go. If you need to scale on many concurrent users editing the same list, things get really complicated and you may need to implement message buses and other goodness. If you find that you need to scale to tens of thousands of items in the list, you'll need to rethink your UI and how it communicates with the server (e.g. you won't want to load the entire list into memory). But when each of the operations is performed manually by a user, worrying about your in-memory data structure isn't going to get you where you want to be in any of these cases.