Home All Groups Group Topic Archive Search About

Sorting Datagrid With Hyphen / Dash

Author
7 Mar 2005 7:42 PM
Chris Mayers via .NET 247
Hi,

I have an dotNet Windows Forms application with a DataGrid.

One of the columns is 'Part Number'

When sorting by this column, the sequence is somthing like:

30-2
30-3
303-1
30-4

ie, for the purposes of the search, the '-' has a very low weighting.

The numbers before the dash represent the type of part, whereas the numbers after the dash represent the size, so What I want is:
30-2
30-3
30-4
303-1

Anyone have any suggestions what I'm doing wrong, or how I can get the behaviour I desire.

PS, I've noticed that Excel and Access seem to sort things the same way...

Regards,

Chris.
--------------------------------
From: Chris Mayers

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>h/q7ZuZ8FUC8cRHASOExFA==</Id>

Author
8 Mar 2005 11:45 PM
Samuel Kim
Due to their sequence in the ASCII set, - (hyphen) comes before the
numbers.
One way you can get around this behavior is to replace the hyphens with
underscore(_) character and do comparison.

If you change the hyphens to underscore, then you will need to revert
the changes once you are done - this you may not like so much.

To overcome this you can implement a class that implements the
IComparator interface and pass it along to the sort mechanism - the
IComparator instance can read the strings replace hyphen to underscore
and then compare. This will not require a change of your original data
and hence may be a cleaner solution - except for the fact that you need
to create another class.
Author
10 Mar 2005 10:07 AM
Chris Mayers
Thanks for your answer, I kind of understand what you are saying, but do you
have any code samples that would make it a little clearer?

However, this still does not really explain exactly what is going on with
the sorting at the moment.
ie

30-199
30-299
303-01
303-02
30-399
30-499

Regardless of the ASCII value of the hyphen, this sort order is WRONG, all
the '30-' codes should be together, and all the '303' codes should be
together. This sequence only makes sense if you ignore the hyphen completly,
ie:

30199
30299
30301
30302
30399
30499

Therefore, I draw the conclusion that tha DataGrid treats the hyphen as a
'special' character for the purposes of sorting. I guess this is because if
you were sorting proper words you would want (say) 'co-operative' to be
sorted next to 'cooperative'. But surely this behaviour should be
controllable...??  :-/

ALL I want is a way of getting the DataGrid (or DataView) to sort the data
with a proper ASCII sort, without any 'special casing' being applied to any
of the characters...

Anyone got any suggestions on that??

Cheers,

Chris.


Show quoteHide quote
"Samuel Kim" <look***@gmail.com> wrote in message
news:1110325517.500209.265740@o13g2000cwo.googlegroups.com...
> Due to their sequence in the ASCII set, - (hyphen) comes before the
> numbers.
> One way you can get around this behavior is to replace the hyphens with
> underscore(_) character and do comparison.
>
> If you change the hyphens to underscore, then you will need to revert
> the changes once you are done - this you may not like so much.
>
> To overcome this you can implement a class that implements the
> IComparator interface and pass it along to the sort mechanism - the
> IComparator instance can read the strings replace hyphen to underscore
> and then compare. This will not require a change of your original data
> and hence may be a cleaner solution - except for the fact that you need
> to create another class.
>