DataGrid or DataView custom sorting

M

Mike Irwin

I set up sorting by various columns for a DataGrid. For one of the columns,
I would like to implement my own sort algorithm, though. For example, the
column contains data like:

<a href="http://www.example.com">DEF</a>
<a href="http://www.test.somedomain">ABC</a>

If I sort by the default algorithm, I get 'DEF' before 'ABC' because the
strings are compared up until they differ after 'www.'. What I would like
to do is somehow sort on the strings 'DEF' and 'ABC'. Is this possible?
Can I somehow implement a custom sort algorithm and then use it from a
DataGrid or DataView?
 
D

Dmitriy Lapshin [C# / .NET MVP]

Hi Mike,

My guess is that you could create a wrapper class for your HTML snippets
that would implement the IComparable interface and ensure the expected sort
order. This class should also override the ToString() method to return the
HTML snipper content, and it should have a constructor creating an instance
from a string passed as well as type conversion operators making it
interchangeable with the System.String class.

An easier solution could exist, but I cannot currently think of anything
simpler. Please treat this as an "off the top of my head" idea.
 
A

Alan Pretre

Mike Irwin said:
I set up sorting by various columns for a DataGrid. For one of the columns,
I would like to implement my own sort algorithm, though. For example, the

Define an object, such as CompareObject, that implements the IComparer
interface. Put your comparison logic for two items to be compared in the
Compare() method.

Then to sort, in the main routine put the other objects to be sorted into an
array, such as ObjectArray, and use Array's Sort() method. Something like

System.Array.Sort(ObjectArray, // Array array
new CompareObject() // IComparer comparer
);

You can bind the sorted array to the DataGrid.

-- Alan
 

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