numbers aren't sorting properly

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
When you click on a column header to sort in ascending or descending order,
numbers (and dates) aren't sorted correctly. It turns up like this:

1
10
11
2
3
4
..
..
..

Is there a way to fix this?

Thanks,
Mel
 
Gonna guess and say change the data type from a string to an int.

Please post more information...is this a web form, win form, are you talking
about a datagrid?
 
melanieab said:
Hi,
When you click on a column header to sort in ascending or descending order,
numbers (and dates) aren't sorted correctly. It turns up like this:

1
10
11
2
3
4
.
.
.

Is there a way to fix this?

They are sorting "properly" - just not usefully. Your sequence is equivalent
lexicographically to:
"1 "
"10"
"11"
"2 ", etc.

The problem is that you are NOT sorting numbers, you are sorting strings which
just happen to contain only the decimal digits "0" through "9". If this is a
listview you are restricted to strings, I believe. In that case you could
format them all to the same length by prepending leading zeroes as per, e.g.,
MyString = MyNum.ToString("000000000"), or else supply your own sort routine.
(Other types of controls may let you specify that your column contains integers,
in which case the built-in sort would handle them OK.)

Similarly, MyString = MyDate.ToString("yyyy-MM-dd HH:mm:ss") would format your
dates so that they would sort the same both as dates and as strings . . .

HTH,
-rick-
 
Thanks! This was the changing to the int thing (in a datagrid). Never even
occurred to me.
 
Back
Top