0

I saw on the property window of listview has a sorting but it works only on string not on integer, my problem is when adding item into listview with an item as Integer and subitem as String, but the result is like this:

  • 1
  • 10
  • 2
  • 3

Whenever I reach the "10" it comes in 2nd order, I searched online but I found only a code for "sort by clicking column" with a bunch of codes. Can you help me with this? Your answer is highly appreciated. Thank you!

James
  • 21
  • 4
  • `an item as Integer` a listview never contains integers, just strings; thats one of the things the DatagridView does better. You need a [ListViewSorter](http://stackoverflow.com/a/35977664/1070452) using a [Natural Sort method](http://stackoverflow.com/a/33786276/1070452) – Ňɏssa Pøngjǣrdenlarp Nov 28 '16 at 15:33

1 Answers1

0

The problem here is sorting according to the ASCIIBetical. You should implement a Icomparer.

Here is a explanation on how it works on the Code Project and here is a reference to the MSDN page

Example on how to sort the list.

Public IntList as New List(of Integer)

Private Sub Test()
    Dim NaturalSorter = New NaturalSort
    IntList.Sort(NaturalSorter)
End Sub

Public Class NaturalSort

    Implements IComparer(Of Object)

    Public Function Comparer(ByVal x As Object,
                             ByVal y As Object) As Integer Implements IComparer(Of Object).Compare

        ' [1] Validate the arguments.
        Dim s1 As String = x
        If s1 = Nothing Then
            Return 0
        End If

        Dim s2 As String = y
        If s2 = Nothing Then
            Return 0
        End If

        Dim len1 As Integer = s1.Length
        Dim len2 As Integer = s2.Length
        Dim marker1 As Integer = 0
        Dim marker2 As Integer = 0

        ' [2] Loop over both Strings.
        While marker1 < len1 And marker2 < len2

            ' [3] Get Chars.
            Dim ch1 As Char = s1(marker1)
            Dim ch2 As Char = s2(marker2)

            Dim space1(len1) As Char
            Dim loc1 As Integer = 0
            Dim space2(len2) As Char
            Dim loc2 As Integer = 0

            ' [4] Collect digits for String one.
            Do
                space1(loc1) = ch1
                loc1 += 1
                marker1 += 1

                If marker1 < len1 Then
                    ch1 = s1(marker1)
                Else
                    Exit Do
                End If
            Loop While Char.IsDigit(ch1) = Char.IsDigit(space1(0))

            ' [5] Collect digits for String two.
            Do
                space2(loc2) = ch2
                loc2 += 1
                marker2 += 1

                If marker2 < len2 Then
                    ch2 = s2(marker2)
                Else
                    Exit Do
                End If
            Loop While Char.IsDigit(ch2) = Char.IsDigit(space2(0))

            ' [6] Convert to Strings.
            Dim str1 = New String(space1)
            Dim str2 = New String(space2)

            ' [7] Parse Strings into Integers.
            Dim result As Integer
            If Char.IsDigit(space1(0)) And Char.IsDigit(space2(0)) Then
                Dim thisNumericChunk = Integer.Parse(str1)
                Dim thatNumericChunk = Integer.Parse(str2)
                result = thisNumericChunk.CompareTo(thatNumericChunk)
            Else
                result = str1.CompareTo(str2)
            End If

            ' [8] Return result if not equal.
            If Not result = 0 Then
                Return result
            End If
        End While

        ' [9] Compare lengths.
        Return len1 - len2

    End Function

End Class
Mech_Engineer
  • 535
  • 1
  • 19
  • 46
  • 1
    Windows has a [built in Natural Sort](http://stackoverflow.com/a/33786276/1070452) – Ňɏssa Pøngjǣrdenlarp Nov 28 '16 at 15:34
  • Thanks for adding that plutonix, I was unaware of this, I will check this in my own project and edit my answer ASAP. – Mech_Engineer Nov 28 '16 at 15:50
  • The "problem" is that the NaturalSort is only part of the answer. In order to use it on a ListView you need to implement it in a ListViewSorter. Your answer is showing how to use it on a list of integer (which would not even need a special sort) rather than the case the OP needs – Ňɏssa Pøngjǣrdenlarp Nov 28 '16 at 15:55
  • wow such a lot of codes, can't read all of that. I'll try that later but may I ask something, if this idea would solve my problem. Maybe it can sort it when the index of an item = value on the item. Something like these: index 1 = item# 2 so index 1 will become index 2 so it will display in order. It's possible with this idea? – James Nov 28 '16 at 16:14