Allow users to sort entries?

I am trying to figure out a way for the user to sort the entries (etc. friend list).

I want to give the user the ability to move a record (of a friend) directly to the top or bottom of the list or by entering a number (between them). A.

At first, I thought about just adding the SortOrder (int) column to the table with all the friends of the users and setting the number in which order the records should be displayed.

But what I'm trying to avoid is that the user has 400 friends, and if he wants to set his friend’s number 400 to position 1 in the list, then I will have to update each individual record with the new SortOrder.

All data is stored in the MS Sql database.

I hope someone has a magical solution for this?

+4
source share
6 answers

For the sort column, use floating point numbers .

Set the starting elements as 0.0, 1.0, etc.

Moving up, use min -1.0. Moving down, the maximum value is + 1.0. Moving between two elements set to (prev + next) /2.0

This is similar to the approach to line numbers, but there is more “space” between them. Theoretically, there is still a point at which you need to renumber when two amended values ​​increase. I have no idea how soon this will happen in practice, but I expect it to be very rare, so that it can be done with any maintenance task.


[edit] FWIW, this problem has come back to me several times, so here is a way that does roughly the same thing, but with strings .

+3
source

I would not have thought that they would do this often enough to be a serious problem, but if you are worried, use the trick that we started with our BASIC code from a few days.

Back when BASIC had line numbers, we just counted them 10, 20, 30, etc. so that if we needed to insert one between 10 and 20, we would call it 15. Or if 20 should have up to 10 , we renumbered it to 5.

With a 32-bit integer column, you can have 200,000 friends at intervals of 100, more than enough to move things around, especially if you're smart.

You might want to start the swap job to renumber your friends to 100, 200, etc. (for example, disk defragmentation for your social network). Do not try to find this by looking at the friends numbers, use another field, setting it to true when the user re-arranges his friends and clears it when defragmenting. It will be more efficient.

+1
source

It looks like you are looking for a structure like a linked list, where each record will contain the identifier of the next record in order.

0
source

I don’t know about magic, but to move up or down, you can simply set SortOrder to MIN / MAX (SortOrder) +/- 1. Who said that the top should be 1 or 0?

0
source

Here's how I do it: use the SortOrder column. Presumably, there will be an initial default sort order, say, in alphabetical order, and so everyone will be assigned a SortOrder value in accordance with their alphabetical order.

Then, when the user has moved someone to the top, you can simply set SortOrder to max +1. If they push someone to the bottom, then it will be min -1. If they moved someone to the middle, then you would like to calculate which half of the middle they are moving to. If this is the upper half, then raise the SortOrder of all above them. If this is the lower half, then reduce the SortOrder for all below.

Not sure if there is a better way to do this ...

0
source

You can look at it as a group of friends.

Initially, everyone is in group 0, and the order is by name or in another way.
- If the user then increases the “Group” of friend (a) to 1, then they move to the top - If the user then increases the “Group” of friend (b) to 1, then (a) and (b) appear at the top
- If the user again increases the "Group" of a friend (b), then (b) appears 1st and (a) 2nd

Just a thousand ...

0
source

All Articles