I’m working with a WPF DataGrid which presents a set of imported files into the program.
Everything works fine but I’ve faced a problem sorting file sizes when the header is clicked!
The normal sorting method cannot differentiate between 12*GB and 12*MB so they appear next to each other and that’s exactly what I don’t want to happen.
Any idea how to solve this problem?
→ Solved the problem by adding a long column even though sizes may look
meaningless I had no choice!
First you need an
IComparer<string>that does the sorting taking into account the suffix. I’m sure you can write your own but here’s a quick-and-dirty approach that just handles the cases you listed:This can be made a lot more efficient if you are sorting many thousands of rows.
Then you need to hook your comparer into the
DataGrid. The following Stack Overflow answer explains it perfectly:You just hook the
DataGrid.Sortingevent and wire up your comparer to theListCollectionViewfor your column.