DataView.Sort Problem

B

BuddyWork

Is this a bug with DataView.Sort

Data
Row0= ""
Row1= "T"
Row2= "S"

Sort Ascending
Returns data as
Row0, Row2, Row1.
Surely it should be Row2, Row1, Row0, this is how Excel
does it.
 
J

Jon Skeet [C# MVP]

BuddyWork said:
Is this a bug with DataView.Sort

Data
Row0= ""
Row1= "T"
Row2= "S"

Sort Ascending
Returns data as
Row0, Row2, Row1.
Surely it should be Row2, Row1, Row0, this is how Excel
does it.

I dont think it's a bug - it just depends on whether you define an
empty string to be before or after all non-empty strings. That's a
matter of preference, really. (In some cases you may want one type of
behaviour, in other cases you may want the other. You may even want
empty strings to come at the end regardless of how the rest is sorted.)
Unfortunately as far as I know there's no way of stating that
preference :(
 
N

Neil Allen

Is this a bug with DataView.Sort

Data
Row0= ""
Row1= "T"
Row2= "S"

Sort Ascending
Returns data as
Row0, Row2, Row1.
Surely it should be Row2, Row1, Row0, this is how Excel
does it.


Dear BuddyWork

Most systems I've come across sort empty strings first - this is just
personal experience and by no means definitive.

Perhaps Excel's sorting is shaped by the possibility that an empty
string could be an empty row.

I know of no configuration option to change the default behavior
(which doesn't mean it doesn't exist).

It might be possible to override the sort behavior in the DataSet /
DataTable / DataView object model somewhere (maybe an implementation
of the IComparer interface somewhere). I'm afraid we don't use
DataSets that extensively.

If the change in sort behavior is critical then a *very* crude
solution might be to add an expression column...

Assuming you originally wanted to sort by a column called
"StringCol"...

sortCol = new DataColumn();
sortCol.DataType = System.Type.GetType("System.String");
sortCol.ColumnName = "SortString";
sortCol.DefaultValue = "";
sortCol.Expression = "iif(StringCol='','zzzzzzzzz',StringCol)";
myTable.Columns.Add(sortCol);

....

DataView myView = new DataView(myTable);
myView.Sort = "SortString";


Regards

Neil Allen



MSDN help on Expressions...

http://msdn.microsoft.com/library/d...fSystemDataDataColumnClassExpressionTopic.asp
 

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