Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does List<T> guarantee insertion order?

Say I have 3 strings in a List (e.g. "1","2","3").

Then I want to reorder them to place "2" in position 1 (e.g. "2","1","3").

I am using this code (setting indexToMoveTo to 1):

listInstance.Remove(itemToMove); listInstance.Insert(indexToMoveTo, itemToMove); 

This seems to work, but I am occasionally getting strange results; sometimes the order is incorrect or items from the list are getting deleted!

Any ideas? Does List<T> guarantee order?

Related:

Does a List<T> guarantee that items will be returned in the order they were added?

like image 243
SuperSuperDev1234 Avatar asked Jun 25 '09 10:06

SuperSuperDev1234


People also ask

Does Python list maintain order of insertion?

Yes, the order of elements in a python list is persistent.

Does order matter in a list Python?

The order of the elements in a list is an intrinsic property of that list and does not change, unless the list itself is modified. (The same is true of tuples, except of course they can't be modified.) The next tutorial will introduce you to the Python dictionary: a composite data type that is unordered. Read on!

Is Order important in list?

Ordered list is used when the order of items are important.

Does array maintain insertion order C#?

Yes it is. Since it's stored as an array. Slow insert and delete in the beginning and middle.


2 Answers

The List<> class does guarantee ordering - things will be retained in the list in the order you add them, including duplicates, unless you explicitly sort the list.

According to MSDN:

...List "Represents a strongly typed list of objects that can be accessed by index."

The index values must remain reliable for this to be accurate. Therefore the order is guaranteed.

You might be getting odd results from your code if you're moving the item later in the list, as your Remove() will move all of the other items down one place before the call to Insert().

Can you boil your code down to something small enough to post?

like image 129
Bevan Avatar answered Sep 20 '22 04:09

Bevan


Here are 4 items, with their index

0  1  2  3 K  C  A  E 

You want to move K to between A and E -- you might think position 3. You have be careful about your indexing here, because after the remove, all the indexes get updated.

So you remove item 0 first, leaving

0  1  2 C  A  E 

Then you insert at 3

0  1  2  3 C  A  E  K 

To get the correct result, you should have used index 2. To make things consistent, you will need to send to (indexToMoveTo-1) if indexToMoveTo > indexToMove, e.g.

bool moveUp = (listInstance.IndexOf(itemToMoveTo) > indexToMove); listInstance.Remove(itemToMove); listInstance.Insert(indexToMoveTo, moveUp ? (itemToMoveTo - 1) : itemToMoveTo); 

This may be related to your problem. Note my code is untested!

EDIT: Alternatively, you could Sort with a custom comparer (IComparer) if that's applicable to your situation.

like image 31
Joel Goodwin Avatar answered Sep 21 '22 04:09

Joel Goodwin