0

I have a ListView (GridView) with multiple columns and so far I can sort it by column alphabetically, but when I'm sorting A-Z, empty strings show up at the top. I want to move these to the end. I think I've managed to make an IComparer that will put empty strings at the end, but I don't know how to make my ListView use it. Here's the comparer I made, by the way:

Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
    If TypeOf x Is String And TypeOf y Is String Then
        If x = "" And y = "" Then
            Return 0
        ElseIf x = "" And y <> "" Then
            Return 1
        ElseIf x <> "" And y = "" Then
            Return -1
        End If
    End If
    Return x.CompareTo(y)
End Function
grant
  • 735
  • 8
  • 17

1 Answers1

1

Check this out. You can wire in a custom sorter of type IComparer, just like you're trying to do: http://ligao101.wordpress.com/2007/07/31/a-much-faster-sorting-for-listview-in-wpf/

J Trana
  • 2,150
  • 2
  • 20
  • 32
  • I tried using the CustomSort on a ListCollectionView like the article says, but it didn't work. My ListView has multiple columns, each of which is bound to a property in a class. I'm using SortDescriptions to sort by a single column, so I'd expect a String (the property type) to be passed to the comparer. It seems to be passing a type of my class, instead. Why??? Also the article says to use .ItemsSource, but that is Nothing/null for me, because I'm setting the DataContext. Could this be affecting it? – grant Apr 06 '11 at 23:20
  • Well the only way I could get this to work was by setting a parameter of my comparer to the property name of the column I'm sorting by and using reflections to get the value of the property, but this breaks multiple column sorting, so I'm making a new question that's a little more specific. http://stackoverflow.com/q/5588353/403736 – grant Apr 07 '11 at 22:26