Collection Sort Performance
-
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
-
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
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#
-
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#
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
-
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
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
-
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
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
-
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
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#