[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 280
  • Last Modified:

Most efficient Structures for sorting

What is the most efficient structure in C# for sorting between various types

For example I have

Strings
Integers
Booleans

As opposed to having them all scattered I want to sort my structure so that I have all strings then integers then booleans.

I was thinking a list would be the simplest solution.
0
directxBOB
Asked:
directxBOB
2 Solutions
 
t_itaniumCommented:
try to use the GetType property ...and sort according to it..

cheers
0
 
anyoneisCommented:
Do they have to go into the same structure? Why not have three separate structures, one for each type? The reason this comes to mind is that if you store all of these together, you are going to have to store objects, and that means you will be boxing and unboxing integers and booleans.

Of course, if they are in the same structure already, that won't be any additional overhead. And the sorting algorithm shouldn't have to box/unbox, so maybe that's not a problem.

What about a simple array of objects?

David
0
 
pallospCommented:
If you store them in an ArrayList, the time complexity of the sorting is
   - 2*n*log2(n) GetType calls
   - n*log2(n) type comparisons
   - n*log2(n) value comparisons
   - 2*n*log2(n) boxings
If you use 3 generic lists for each type
   - n GetType calls and n boxings for distributing the elements into 3 lists
   - n/3 * log2(n/3) value comparisons for each list
   - n boxings for putting all together in 1 list

I think sorting using generic lists is 2-3 times faster.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now