Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7711387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:21:45+00:00 2026-06-01T01:21:45+00:00

does List Sort modify the collection? I think it must as I get a

  • 0

does List Sort modify the collection?

I think it must as I get a “System.InvalidOperationException: Collection was modified; enumeration operation may not execute.” exception on another thread.

In my multi-threaded app all threads I thought were just reading the collection BUT one thread does a sort.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T01:21:46+00:00Added an answer on June 1, 2026 at 1:21 am

    Yes, Sort is in-place, if that’s what you mean, and it will certainly invalidate any iterators.

    If you want to see a sorted “view” of the collection, you can use LINQ’s OrderBy operator, which doesn’t modify the existing collection but returns a sequence which contains the elements from the original collection, but in the given order.

    So for example, instead of:

    // I want to print out the list of names, sorted...
    names.Sort();
    foreach (string name in names)
    {
        Console.WriteLine(name);
    }
    

    You could use:

    foreach (string name in names.OrderBy(x => x))
    {
        Console.WriteLine(name);
    }
    

    Another alternative is just to sort it once when you first populate the list, before anything starts iterating over it – that’s the only modification required, and if the sort order won’t change (e.g. due to modifications to the objects referred to in the list) then it would make sense to just do it once.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got a list i.e. ls= [u'Cancer',uMen's,u'Orthopedics',u'Pediatric',uSenior's,uWomen's] ls.sort() does not seem to work
I keep getting a 'List does not exist' error whenever I try and use
Does anyone know of a List implementation that has a constant time get(int index)
Possible Duplicate: Is std::list<>::sort stable? Does C++ std::list sort function is guaranteed to preserve
Does anyone know how to sort a list of struct with different types (sample
How does one sort a list in Erlang depending on a tag for each
Does a List<T> always guarantee that items will be returned in the order they
Does len(list) calculate the length of the list every time it is called, or
Why does this work: List<?> list = new LinkedList<Integer>(); while this gives a type
Does anyone know the full list of C# compiler number literal modifiers? By default

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.