1

I'm using a List and sort items.

objListOrder.Sort(
delegate(Order p1, Order p2)
{
    return p1.Title.CompareTo(p2.Title);
}

I'm having an issue where I need to disable sort and order the items in the order they were added in the list, not in the alphabetical order of corresponding keys.

Is there a way to clear sorting after sort?

coder
  • 3,195
  • 2
  • 19
  • 28

3 Answers3

2

Is there a way to clear sorting after sort?

No. When you need to use a sorted list use

var newList = objListOrder.OrderBy(p=>p.Title).ToList();

Which creates a sorted copy of the original List

EZI
  • 15,209
  • 2
  • 27
  • 33
1

Is there a way to clear sorting after sort?

No, lists do not keep information about the order in which their items were added.

But of course you could add an extra field like OriginalIndex to you object and sort the list by this field.

AlexD
  • 32,156
  • 3
  • 71
  • 65
0

It sounds like what you are after here is for a call to the Sort method to have no affect. Given List<T> guarantees ordering just have your comparer delegate return 0, this effectively tells the list that the items should not be shifted

delegate(Order p1, Order p2) {
    return 0;
}

See Comparison<>

James
  • 80,725
  • 18
  • 167
  • 237