Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Collection Sort Performance

Collection Sort Performance

Scheduled Pinned Locked Moved C#
comdata-structuresperformancequestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mark Sanders
    wrote on last edited by
    #1

    I am creating a strongly-typed collection class for a custom class using the CollectionBase class. I have added a Sort() function to the class with the following description:

    public void Sort()
    {
        myClass[] tempArray = new myClass[List.Count];
        tempArray.Initialize();
        List.CopyTo(tempArray, 0);
    
        Array.Sort(tempArray);
    
        List.Clear();
        foreach(myClass val in tempArray)
            List.Add(val);
    }
    

    Is there a better way to do this, performance wise? Or is this implementation a decent one? FYI: The list count for this collection will be very large when used in the application. Mark Sanders sanderssolutions.com

    D J 2 Replies Last reply
    0
    • M Mark Sanders

      I am creating a strongly-typed collection class for a custom class using the CollectionBase class. I have added a Sort() function to the class with the following description:

      public void Sort()
      {
          myClass[] tempArray = new myClass[List.Count];
          tempArray.Initialize();
          List.CopyTo(tempArray, 0);
      
          Array.Sort(tempArray);
      
          List.Clear();
          foreach(myClass val in tempArray)
              List.Add(val);
      }
      

      Is there a better way to do this, performance wise? Or is this implementation a decent one? FYI: The list count for this collection will be very large when used in the application. Mark Sanders sanderssolutions.com

      D Offline
      D Offline
      Daniel Turini
      wrote on last edited by
      #2

      Mark Sanders wrote: Is there a better way to do this, performance wise? Or is this implementation a decent one? Maybe there is another way of doing it, but the only one that passes through my mind now is the SortedList container. Do you really need to have a Sort method or can the list be sorted all the time? If this is the case, then deriving from or aggregating a SortedList can give you the best performance. My latest article: GBVB - Converting VB.NET code to C#

      M 1 Reply Last reply
      0
      • D Daniel Turini

        Mark Sanders wrote: Is there a better way to do this, performance wise? Or is this implementation a decent one? Maybe there is another way of doing it, but the only one that passes through my mind now is the SortedList container. Do you really need to have a Sort method or can the list be sorted all the time? If this is the case, then deriving from or aggregating a SortedList can give you the best performance. My latest article: GBVB - Converting VB.NET code to C#

        M Offline
        M Offline
        Mark Sanders
        wrote on last edited by
        #3

        The collection cannot be automatically sorted. In use, the collection will usually be unsorted. Only on occasion will sorting be necessary. Thanks. Mark Sanders sanderssolutions.com

        1 Reply Last reply
        0
        • M Mark Sanders

          I am creating a strongly-typed collection class for a custom class using the CollectionBase class. I have added a Sort() function to the class with the following description:

          public void Sort()
          {
              myClass[] tempArray = new myClass[List.Count];
              tempArray.Initialize();
              List.CopyTo(tempArray, 0);
          
              Array.Sort(tempArray);
          
              List.Clear();
              foreach(myClass val in tempArray)
                  List.Add(val);
          }
          

          Is there a better way to do this, performance wise? Or is this implementation a decent one? FYI: The list count for this collection will be very large when used in the application. Mark Sanders sanderssolutions.com

          J Offline
          J Offline
          James T Johnson
          wrote on last edited by
          #4

          Since you're inheriting from CollectionBase you can use the underlying ArrayList's Sort method. That will (should anyway) in turn call the underlying Array's Sort method. You can access the ArrayList by the protected InnerList property on the CollectionBase. James "It is self repeating, of unknown pattern" Data - Star Trek: The Next Generation

          L 1 Reply Last reply
          0
          • J James T Johnson

            Since you're inheriting from CollectionBase you can use the underlying ArrayList's Sort method. That will (should anyway) in turn call the underlying Array's Sort method. You can access the ArrayList by the protected InnerList property on the CollectionBase. James "It is self repeating, of unknown pattern" Data - Star Trek: The Next Generation

            L Offline
            L Offline
            leppie
            wrote on last edited by
            #5

            James T. Johnson wrote: Since you're inheriting from CollectionBase you can use the underlying ArrayList's Sort method. That will (should anyway) in turn call the underlying Array's Sort method. It actually calls an quicksort implementation. Saw it many moons back. Quite fast as well. Hey leppie! Your "proof" seems brilliant and absurd at the same time. - Vikram Punathambekar 28 Apr '03

            D 1 Reply Last reply
            0
            • L leppie

              James T. Johnson wrote: Since you're inheriting from CollectionBase you can use the underlying ArrayList's Sort method. That will (should anyway) in turn call the underlying Array's Sort method. It actually calls an quicksort implementation. Saw it many moons back. Quite fast as well. Hey leppie! Your "proof" seems brilliant and absurd at the same time. - Vikram Punathambekar 28 Apr '03

              D Offline
              D Offline
              Daniel Turini
              wrote on last edited by
              #6

              leppie wrote: Quite fast as well. If the data is not already sorted (which is the worst case of the QuickSort algorithm) My latest article: GBVB - Converting VB.NET code to C#

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups