Sorting a listview(numericly)

ok.. i'm aware that a listview has a sort and i know how to use it. but my problem is is that it sorts textually. how do i sort numeric data? should i code a bubble sort myself?
hessAsked:
Who is Participating?
 
mcriderConnect With a Mentor Commented:
Yes, you have to do the sort yourself.  The listview control will not do a numeric sort...


See the following microsoft KB article:

HOWTO: Sort a ListView Control by Date
http://support.microsoft.com/support/kb/articles/q170/8/84.asp?LNG=ENG&SA=MSDN 
 
I've also attached a microsoft KB article that apparently no longer exists... It gives an example of sorting and is in VB4 but works in VB5/6

Cheers!



SAMPLE: Sorting Algorithms for Numeric Arrays

--------------------------------------------------------------------------------
The information in this article applies to:

Microsoft Visual Basic Standard, Professional, and Enterprise Editions, 16-bit and 32-bit, for Windows, version 4.0

--------------------------------------------------------------------------------


SUMMARY
Sorted.exe is a sample that demonstrates how to sort algorithms for numeric arrays. This article describes the three methods of sorting a series of numbers in a code and provides sample code showing how to implement them.

The following file is available for download from the Microsoft Software Library:

~ Sorted.exe

For more information about downloading files from the Microsoft Software Library, please see the following article in the Microsoft Knowledge Base:


Q119591 : How to Obtain Microsoft Support Files from Online Services





MORE INFORMATION
It is often necessary to sort a series of numbers in code and there are various sorting algorithms available to do this.

The three methods discussed are:


Bubble sort.


Selection sort.


Shell sort.



Each routine simply receives an array full of numbers within the bounds of a LONG datatype, although this could be easily changed for different numeric datatypes. Note that the sort routines return the result of the sort operation in the array passed to the routine. Therefore, if you do not want the original array to be modified, copy the array to a variant, as shown in the example below, and then pass the variant to the relevant procedure.


Step-by-Step Example
Start a new project. Form1 is created by default.


Place a Command Button on the form.


Add the following code to the Form1 code window:
   Option Explicit

   sub Command1_Click()
     Dim lMyArray(0 TO 9) As Long
     Dim vTemp1 As Variant
     Dim vTemp2 As Variant
     Dim vTemp3 As Variant
     Dim iLoop As Integer
     Randomize
     For iLoop = LBound(lMyArray) To UBound(lMyArray)
       lMyArray(iLoop) = Int(Rnd * 100) + 1
     Next iLoop
     vTemp1 = lMyArray
     vTemp2 = lMyArray
     vTemp3 = lMyArray
     Call BubbleSortNumbers(vTemp1)
     Call SelectionSortNumbers(vTemp2)
     Call ShellSortNumbers(vTemp3)
   end sub

   Sub BubbleSortNumbers(iArray As Variant)
     Dim lLoop1 As Long
     Dim lLoop2 As Long
     Dim lTemp As Long
     For lLoop1 = UBound(iArray) To LBound(iArray) Step -1
       For lLoop2 = LBound(iArray) + 1 To lLoop1
         If iArray(lLoop2 - 1) > iArray(lLoop2) Then
           lTemp = iArray(lLoop2 - 1)
           iArray(lLoop2 - 1) = iArray(lLoop2)
           iArray(lLoop2) = lTemp
         End If
       Next lLoop2
     Next lLoop1
   End Sub

   Sub SelectionSortNumbers(vArray As Variant)
     Dim lLoop1 As Long
     Dim lLoop2 As Long
     Dim lMin As Long
     Dim lTemp As Long
     For lLoop1 = LBound(vArray) To UBound(vArray) - 1
       lMin = lLoop1
         For lLoop2 = lLoop1 + 1 To UBound(vArray)
           If vArray(lLoop2) < vArray(lMin) Then lMin = lLoop2
         Next lLoop2
         lTemp = vArray(lMin)
         vArray(lMin) = vArray(lLoop1)
         vArray(lLoop1) = lTemp
     Next lLoop1
   End Sub

   Sub ShellSortNumbers(vArray As Variant)
     Dim lLoop1 As Long
     Dim lHold As Long
     Dim lHValue As Long
     Dim lTemp As Long
     lHValue = LBound(vArray)
     Do
       lHValue = 3 * lHValue + 1
     Loop Until lHValue > UBound(vArray)
     Do
       lHValue = lHValue / 3
       For lLoop1 = lHValue + LBound(vArray) To UBound(vArray)
         lTemp = vArray(lLoop1)
         lHold = lLoop1
         Do While vArray(lHold - lHValue) > lTemp
           vArray(lHold) = vArray(lHold - lHValue)
           lHold = lHold - lHValue
           If lHold < lHValue Then Exit Do
         Loop
           vArray(lHold) = lTemp
       Next lLoop1
     Loop Until lHValue = LBound(vArray)
   End Sub
 




Additional query words: 4.00 kbDSupport

Keywords          : kbsample kbVBp400 VB4WIN
Version           : 4.00 | 4.00
Platform          : NT WINDOWS
Issue type        :
0
 
crazymanCommented:
you can sort numerically using the same method i have used the following code which also has a little arrow icon that faces up or down when you click the columns



Private Sub LV_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)
On Error GoTo e
    Dim c As ColumnHeader
    Static LastColClicked As ColumnHeader
    LV.SortKey = ColumnHeader.Index - 1 ' change the sort key to the clicked header
    If LastColClicked Is Nothing Then
        GoTo d
    ElseIf ColumnHeader = LastColClicked Then
        GoTo d
    Else
        ColumnHeader.Icon = LastColClicked.Icon
        LastColClicked.Icon = "N"
    End If
    Set LastColClicked = ColumnHeader
Exit Sub
d:
   If LV.SortOrder = lvwDescending Then
            For Each c In LV.ColumnHeaders
                 c.Icon = "N"
            Next c
            'set all icons to N from image list
            ColumnHeader.Icon = "U" ' set selected icon to up arrow
            LV.SortOrder = lvwAscending 'change sort order
    Else
            For Each c In LV.ColumnHeaders
                c.Icon = "N"
            Next c
            ColumnHeader.Icon = "D"
            LV.SortOrder = lvwDescending
    End If
    Set LastColClicked = ColumnHeader
Exit Sub
e:
Log Err.Description, "ColumnClick"
End Sub

the key is this line
 LV.SortKey = ColumnHeader.Index - 1

which changes the sort column,if it is numeric then it will sort numerically.
0
 
crazymanCommented:
Im a bit lost here, i have a list view and ive no sorting code aside from what i posted and it has numeric,dates,and text and they all sort fine without any additional code.
What is the reason for this ?
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
hessAuthor Commented:
crazyman... your sort sort strings ... your sort does this

1
11
2
22
3
33

where i want

1
2
3
11
22
33
0
 
hessAuthor Commented:
mcrider... yours looks good i'll try yours when i get home. i'm on lunch break now.
0
 
crazymanCommented:
your correct , <g> i never looked properly
0
 
crazymanCommented:
but what you can do is place 0's in front of the numbers so youd get


001
002
003
011
022
033

not

001
011
002
022
003
033


0
 
mcriderCommented:
Thanks for the points! Glad I could help!


Cheers!
0
All Courses

From novice to tech pro — start learning today.