Custom Sort Order

E

Ethan Strauss

Hi,
I want to be able to create a custom sort order for a Sorted List.
Specifically, I have a grid which goes from A1 to H12. The default sort
gives me A10, A11, A1, A2 ... I would like to change it so that it first
sorts by the alphabetical character and then the number. I have figured out
that I need to use IComparer, but I can't figure how to set up IComparer.
Can anyone help?
Thanks!
Ethan
 
R

rossum

Hi,
I want to be able to create a custom sort order for a Sorted List.
Specifically, I have a grid which goes from A1 to H12. The default sort
gives me A10, A11, A1, A2 ... I would like to change it so that it first
sorts by the alphabetical character and then the number. I have figured out
that I need to use IComparer, but I can't figure how to set up IComparer.
Can anyone help?
Thanks!
Ethan
The simplest way may be to change the names of the grid. A01, A02,
A03 etc. will sort correctly.

rossum
 
M

Michael D. Ober

In pseudo-code:

Split this into alpha and numeric components.
Split "Other" into alpha and numeric components.
int retval = thisAlpha.CompareTo(otherAlpha)
if retval <> 0 return retval
return int.parse(thisNumeric).compareto(int.parse(otherNumeric))

Basically, what you are doing is comparing the alpha parts first and if they
are equal, converting the numeric parts to integers and comparing them.

Mike.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top