views:

623

answers:

1

If I have two strings, and I need to compare them in ASCII order (instead of the invariant culture order) I can generally use:

int c = String.Compare(a, b, StringComparison.Ordinal);

If I have a DataTable, and I want one of the columns sorted using an Ordinal string comparison for the DataView , how do I go about it?

+1  A: 

Unfortunately, there's nothing built-in to facilitate custom sorting logic in the DataView; that's been one of my chief complaints, and one of the reasons that we decided to roll our own data access layer at my old job.

While I don't know how feasible this would be for you to do, to really achieve this you'd need to add another column to your table as an int, then sort the rows in an array based upon your own comparison logic, then store the index in the array in your int column. Then, rather than ordering by the string column order by the int column.

I do recognize, however, that this doesn't satisfy things like automatic sorting that gets done by a GridView and it doesn't account for changes to the data that take place after you calculate the sorting positions. Unfortunately, I don't think there's anything that can really help with this.

If you're looking to sort this visually, check and see if your visual control supports a custom sorting mechanism. That's about all I can suggest.

Adam Robinson