Listview sort order

I have a listview with numbers in the first column which I would like to order sequentially.  At the moment my order is

1
10
100
1000
2
20
200
3
30
300

and so on,  I need it to be

1
2
3
10
20
30
100
200
300
1000

and so on.  Im guessing this is relatively simple but how do I achieve it?
LVL 1
simonwaitAsked:
Who is Participating?
 
DhaestConnect With a Mentor Commented:
Several ways to perform it:
1) use a MSFlexGrid, that allows the selection of the type of ordering, for each column (not only: it also allows to order using only a selected range of rows)
2) "pad" the items with blank characters to the left, so that the sort weel be correct even using an alphanumeric ordering. For eample, if you know that the numbers will have a maximum of 6 digits only, put the number into a string and them convert it using:
dim k as long
k = 6 - len(myString)
if k > 0 then myString = Space(k) & myString
3) listview sorting using API.
4) Say, your column name id Id, which is numeric, and assume that your id goes maximum upto 9999 then on Id column click, you can do the following: when you are loading the list view, give Format(!Id, "0000")
5) If you are getting the data from a database, it would be easier to sort it before you loaded it into the Listview.

Source: http://forums.devx.com/showthread.php?t=153973
0
 
DhaestCommented:
ListView - Sorting by Number or Date
http://www.codeguru.com/vb/controls/vb_listview/article.php/c1669


(by the way: is is .net or classic vb6 ?)
0
 
simonwaitAuthor Commented:
So I think that should give me the following code however not im struggling with the "With" just before the sort (with objcurrli)

Oh its classic vb6
Private Sub LoadCustomerListView()
'-----------------------------------------------------------------------------
                                 
    Dim strSQL      As String
    Dim objCurrLI   As ListItem
    Dim strZip      As String
    Dim strPhone    As String
    Dim cueno As String
    Dim lngindex As Integer
                                 
    strSQL = "SELECT * FROM Act1 ORDER BY Q"
    ' "SELECT FirstName" _
           & "     , LastName" _
           & "     , Address" _
           & "     , City" _
           & "     , State" _
           & "     , Zip" _
           & "     , PhoneNumber" _
           & "     , CustID" _
           & "  FROM Act 1 " _
           & " ORDER BY Q" _

    
    mobjCmd.CommandText = strSQL
    Set mobjRst = mobjCmd.Execute
    
    lvwCustomer.ListItems.Clear
    
    With mobjRst
                cueno = ""
        Do Until .EOF
            Set objCurrLI = lvwCustomer.ListItems.Add(, , !Qno)
            If cueno = !Qno Then
            objCurrLI.SubItems(mlngQ_No_IDX) = "" & ""
            objCurrLI.SubItems(mlngQ_Number_IDX) = "" & ""
            objCurrLI.SubItems(mlngQ_Initiator_IDX) = "" & ""

            Else
            objCurrLI.SubItems(mlngQ_No_IDX) = !Q & ""
            objCurrLI.SubItems(mlngQ_Number_IDX) = !Initiator & ""
            objCurrLI.SubItems(mlngQ_Initiator_IDX) = !Description & ""
            End If
            objCurrLI.SubItems(mlngQ_Description_IDX) = !PB & ""
            objCurrLI.SubItems(mlngQ_PB_IDX) = !Axis & ""
            objCurrLI.SubItems(mlngQ_Axis_IDX) = !Dead & ""
            objCurrLI.SubItems(mlngQ_Dead_IDX) = !Pos & ""
            objCurrLI.SubItems(mlngQ_Position_IDX) = !Delay & ""
            objCurrLI.SubItems(mlngQ_Delay_IDX) = !Accel & ""
            objCurrLI.SubItems(mlngQ_Accel_IDX) = !Decel & ""
            objCurrLI.SubItems(mlngQ_Decel_IDX) = !Speed & ""
            objCurrLI.SubItems(mlngQ_Speed_IDX) = !CopyCalcTime & ""
            objCurrLI.SubItems(mlngQ_CopyCalc_IDX) = !StartPos & ""
            objCurrLI.SubItems(mlngQ_StartPos_IDX) = !TravelDist & ""
            objCurrLI.SubItems(mlngQ_TravelDistance_IDX) = !MoveTime & ""
            objCurrLI.SubItems(mlngQ_Time_IDX) = !Moveincdelay & ""
           ' objCurrLI.SubItems(mlngQ_TimeincDelay_IDX) = !Movetime
               
'            objCurrLI.SubItems(mlngCUST_ID_IDX) = CStr(!CustID)
cueno = !Qno
            .MoveNext
        Loop
    End With
    lngindex = 0
      
            With .objCurrLI
                If (lngindex > 0) Then
                    For l = 1 To .Count
                        With .Item(l).ListSubItems(lngindex)
                            .Tag = .Text & Chr$(0) & .Tag
                            If IsNumeric(.Text) Then
                                If CDbl(.Text) >= 0 Then
                                    .Text = Format(CDbl(.Text), _
                                        strFormat)
                                Else
                                    .Text = "&" & InvNumber( _
                                        Format(0 - CDbl(.Text), _
                                        strFormat))
                                End If
                            Else
                                .Text = ""
                            End If
                        End With
                    Next l
                Else
                    For l = 1 To .Count
                        With .Item(l)
                            .Tag = .Text & Chr$(0) & .Tag
                            If IsNumeric(.Text) Then
                                If CDbl(.Text) >= 0 Then
                                    .Text = Format(CDbl(.Text), _
                                        strFormat)
                                Else
                                    .Text = "&" & InvNumber( _
                                        Format(0 - CDbl(.Text), _
                                        strFormat))
                                End If
                            Else
                                .Text = ""
                            End If
                        End With
                    Next l
                End If
            End With

        
    

End Sub

Open in new window

0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
EDDYKTCommented:
try

With objCurrLI
not
With .objCurrLI
0
 
ZhaolaiCommented:
An alternative is to add a hidden column with the same data but formatted like "00000000", then you can sort the hidden column as normal sorting.
0
 
simonwaitAuthor Commented:
I actually used the 2nd suggestion (padding) the column which w2as sorted on was already a hidden column so not too much of a leap to use the padding method
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.