0

I would like to know if the method List<>.Sort actually changed the list or if its untouched.

I only came up with the following solution which doesn't feel like the most ideal there could be:

var fooList = new List<Foo>();
var tempList = fooList.ToList();

fooList.Sort(new FooComparer());

if (fooList.SequenceEqual(tempList))
{
    //Sequence is same...
}
else
{
    //Sequence is different...
}

Hope there is something faster than this, than already the Sort takes quite some time with the data at hand and now the additional tempList and going threw both collections in Enumerable.SequenceEqual, just gives me goose bumps.

Rand Random
  • 7,300
  • 10
  • 40
  • 88
  • 1
    Given that `List.Sort()` does not have a return value, what good would it be if it didn't actually change the list? If it were not allowed to mutate the list, it would be a no-op. (Or generate a new, sorted list, but throw it away without you ever seeing it, which is of course complete non-sense.) – stakx - no longer contributing Apr 15 '15 at 14:43
  • WPF has the ObservableCollection class, I believe it has built in notification when the collection changes – reggaeguitar Apr 15 '15 at 14:43
  • @reggaeguitar ObservableCollection only fires CollectionChanged for Remove/Add/Reset and not if the items in the list gets sorted – Rand Random Apr 15 '15 at 14:44
  • The fastest way is probably to reimplement the `Sort` and save this information somewhere. – xanatos Apr 15 '15 at 14:46
  • @stakx If the list is already ordered, and there are no items with the same value (so that the non-stableness of Sort can't modify them), then Sort could not-modify the List – xanatos Apr 15 '15 at 14:47
  • @xanatos: My comment addresses just the very first question of the OP. And that wasn't about "what if the list is already sorted?"; it was "does List.Sort() change the list?" Granted, that's a very narrow view. But I'd like to hint at the fact that the question doesn't actually state what the OP is apparently really interested in (an instance of the XY problem, I guess). – stakx - no longer contributing Apr 15 '15 at 14:50
  • It would be more code but the most efficient would be to do compare of items one by one and as soon as a compare is different return a boolean for the list not being sorted. If the list is not sorted you know it will be different if you sort. – Franck Apr 15 '15 at 14:51

1 Answers1

3

It sounds like you simply want to check whether an existing list is already ordered or not.

You can do that much more simply; just loop through the list, and check whether any element is less than its predecessor.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 1
    For example, apply [some kind of `.Pairwise()` custom LINQ operator](http://stackoverflow.com/questions/577590/pair-wise-iteration-in-c-sharp-or-sliding-window-enumerator) to the original sequence, then combine with `.All(...)`: `bool isSorted = list.Pairwise().All((a, b) => a <= b)` – stakx - no longer contributing Apr 15 '15 at 14:47
  • 2
    @stakx It is a `List<>`... Linq shouldn't be the solution for all the problems of humanity. A `for (...)` cycle is more than enough. – xanatos Apr 15 '15 at 14:52
  • @xanatos: Of course LINQ isn't the solution for all problems of humanity. But when it comes to querying sequences / collections, and you happen to like a declarative (rather than imperative) approach, then LINQ is usually a very good fit (because that's one of its principal uses). My main intent here was not to advertise LINQ, but to augment the answer with a code example; feel free to suggest an alternative example. – stakx - no longer contributing Apr 16 '15 at 07:14
  • @stakx If you want to use linq, at least use standard operators: `bool isSorted = list.Count < 2 || Enumerable.Range(0, list.Count - 1).All(x => list[x] <= list[x + 1])` :-) – xanatos Apr 16 '15 at 07:20
  • @xanatos: Fair enough. To me personally it's mostly a personal preference: Standard operators are fine of course, but If I see a way to make a query easier to read (again, that's subjective) with a custom LINQ operator, then I go ahead and implement one. But for educational purposes, you're right: it's probably better to stick with the standard operators. – stakx - no longer contributing Apr 16 '15 at 08:03
  • One advantage to using LINQ is that it can be easily parallelized – reggaeguitar Apr 20 '15 at 21:18