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 Position
s are equal.
Take the following list (P=Position,I=Id):
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
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)
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
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
Do positions needs to be contiguous?
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
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