Sorting problem

G

Guest

Hi All,

I need to loop through a sorted datatable. Looping is not a problem, but
sorting is.

The datacolumns on which I want to sort, exists of numbers, which causes the
10 to appear before the 2.

This is the code I use to sort the table

DataRow[] drarray = myTable.Select("", "VerticalPosition asc,
HorizontalPosition asc", DataViewRowState.CurrentRows);

Anyone an idea how I can sort the table correctly?

Thanks in Advance,

Michel
 
C

Cor Ligthert

Michel,

You mean something as this?
\\\
DataView dv = new DataView(dt);
dv.Sort = "bla"; // sorts column bla
DataTable dtnew = dt.Clone();
foreach (DataRowView dvr in dv)
dtnew.ImportRow(dvr.Row);
dt.Clear();
dt = dtnew.Copy();
///

Cor
 
O

Olorin

hmmm...seems you are using string sorting (which puts '10' before '2'),
whereas it sounds like you'd like to sort as if these were integer
values.

If the values in this column are always integers, why not have that
column in the set hold integers? That might work.

If not, you may have to do your own sorting, casting all of them to
integers, and doing...oh I don't know...a bubble sort on the whole data
set based on the value of the column once it's cast to int?
HTH,
F.O.R.
 
G

Guest

Hi,

Indeed, I'm sorting a string column. If filled in, the values shall alway's
be int's. But they can be blank. (Dataset is generated from an xml file, and
not alway's a value is provided).

Maybe adding an int column and sorting on that column may do the trick.
Gonna try it out.

Kind regards,

Michel
 
B

Bruce Wood

If you have the ability to change the XML schema, just change the
attribute or element type for the "column" to be an integer. If the
value is not supplied then ADO.NET will set the column value for that
row to be System.DBNull, and sorting should work correctly.

After all, if the data is not supplied, then it's "null", not blank,
right? Blank means that the tag was there in the XML but its value was
an empty string. If this is your case (if the XML sometimes contains
the tag or attribute but with an empty-string value) or if you have no
control over the schema, then you'll have to use one of the other
workarounds mentioned here.
 
J

James Curran

Have the Database put the data into a workable format for you. Instead of :

Select VerticalPosition, HorizontalPosition, x, y, z
from mytable
where ....

make it:

Select VerticalPosition, HorizontalPosition, x, y, z,
ISNULL(CAST(VerticalPosition as Int), -1) as sortVerticalPosition,
ISNULL(CAST(HorizontalPosition as Int), -1) as sortHorizontalPosition,
from ....

then use:
DataRow[] drarray = myTable.Select("", "sortVerticalPosition asc,
sortHorizontalPosition asc",
DataViewRowState.CurrentRows);

Replace the "-1"s in that to something that is reasonable to a NULL
value --- presumably some very high or very low value.
 

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