?
Solved

Listview sort order

Posted on 2010-11-30
6
Medium Priority
?
291 Views
Last Modified: 2012-05-10
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?
0
Comment
Question by:simonwait
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 34239497
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
 
LVL 1

Author Comment

by:simonwait
ID: 34239594
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
 
LVL 26

Expert Comment

by:EDDYKT
ID: 34240884
try

With objCurrLI
not
With .objCurrLI
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Expert Comment

by:Zhaolai
ID: 34242243
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
 
LVL 53

Accepted Solution

by:
Dhaest earned 2000 total points
ID: 34245735
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
 
LVL 1

Author Closing Comment

by:simonwait
ID: 34247483
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month14 days, 21 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question