C
C#5mo ago
Kiel

How to move an item in a "sorted" list above another item, preserving the sorting?

This is less of a C#-specific question, but my brain is really struggling to comprehend this. I'm operating on a double-sorted collection, where items are sorted first by their Position, and then by their Id if Positions are equal. Take the following list (P=Position,I=Id):
A - P1I1
B - P2I1
C - P3I1
D - P4I1
E - P4I2
F - P5I1
G - P5I2
...imagine a similar pattern continuing
A - P1I1
B - P2I1
C - P3I1
D - P4I1
E - P4I2
F - P5I1
G - P5I2
...imagine a similar pattern continuing
Let's say I want to move C between D and E. What do I need to do to ensure that C is now sorted between D and E, given: - Id cannot be modified - Position can be modified, but cannot go below 1 - Sorting order should be preserved for all other items if possible (IE, E and above should still be in the same order as before) - Position and Id have no relation, it's just how what I am working with sorts them Sorry, I know this sounds like some stupid programming class homework, but it's a real life issue I'm dealing with my Discord bot right now with re-ordering roles and I wanted to try to simplify it as much as possible.
6 Replies
Omnissiah
Omnissiah5mo ago
but why would you move C since it's already ordered? or am i losing some context or you mean changing its properties P/I to move it between D and E like if D and E have equal P then you have to recreate all I after the one you are taking place of or not recreate if Id cannot be modified but just moved i guess? or if I cannot be modified like really cannot then you have to regenerate all Ps (after C -- eventually included C)
Kiel
KielOP5mo ago
the context is that I want to move (Discord) role C between roles D and E. To do so via the API requires modifying all/any affected roles' positions accordingly such that the new order is as you want it to be, meaning that I would need to modify any potentially affected roles such that C's position is effectively between D and E And I'm unsure if there is a more elegant way to do this other than increasing every role from E and above's position by 1, and then swapping C and D's position, resulting in
A - P1I1
B - P2I1
D - P3I1
C - P4I1
E - P5I2
F - P6I1
G - P6I2
...
A - P1I1
B - P2I1
D - P3I1
C - P4I1
E - P5I2
F - P6I1
G - P6I2
...
Omnissiah
Omnissiah5mo ago
i think you should show more clearly that there are no relations between P and I you can try find holes in the Ps, but other that
canton7
canton75mo ago
Do positions needs to be contiguous?
Kiel
KielOP5mo ago
I honestly don't know if Discord normalizes them, so whether or not they do, let's say no, they don't, but it's preferred if possible
Anton
Anton5mo ago
you only need to do that unless you don't have to ig you should look at the values before and after if there's a gap if there's no gap, you have to move the ones after first check if the id fits if it doesn't, see if you can stick another position in between D and E if you can't, update E's position then normalize move position 2 to 1 if possible maybe leave gaps, maybe don't leave gaps it depends on what you wnat
Want results from more Discord servers?
Add your server