tags:

views:

125

answers:

1

I use the streamreader to fill a datagridview. I need to sort the datagridview integer-wise and have tried to use the SortCompare event. To compare the row values i use the function

Public Function CompareIntegers(ByVal value1 As String, ByVal value2 As String) As Integer
    Dim int1 As Integer = Integer.Parse(value1)
    Dim int2 As Integer = Integer.Parse(value2)
    If int1 > int2 Then
        Return 1
    Else
        Return -1
    End If
End Function

Which i call from the SortCompare event with

Private Sub DGV_SortCompare(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewSortCompareEventArgs)           
    e.SortResult = CompareIntegers(e.CellValue1, e.CellValue2)
End Sub

The event is fired but the datagridview is still sorted as strings. Is this the correct approach, and if so what am i missing?

A: 

You're not returning 0 when they are equal

Ruben Bartelink
No, you are right. But in this specific case the values will never be equal, but even if I return 0 for equal values the values are still sorted as strings.
Kristoffer Rigbolt
Fine, but as part of some types of sort algorithm, a redundant compare to-self can sometimes occur, so its defintely A Bad Idea to not handle the equal case.
Ruben Bartelink