ListView Sorting VB6

I am using the Listview control shipped with VB6 and would like to sort the data.

I have setup the ListView in lvwReport mode and have multiple columns.

I know I can sort by column, but is it possible to sort this control using more than one column at a time?  I would like to sort using the Text property as the primary sort, and SubItem(1) as the secondary sort.

I need the results to be like this:

"AAAA"    "BBBB"
"AAAA"    "DDDD"
"AAAA"    "ZZZZ"
"BBBB"    "DDDD"
"BBBB"    "XXXX"

Where the second column is sorted as well.

That's all.
LVL 3
nichiaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JR2003Commented:
Just sort by column 2 first and then by column 1.

ListView1.SortOrder = lvwAscending
ListView1.Sorted = False
ListView1.SortKey = 1
ListView1.Sorted = True
ListView1.SortKey = 0
ListView1.Sorted = True
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
amebaCommented:
Hi nichia,
here's another way:
1) add temporary column (width=0)
        Dim ch As ColumnHeader
        Set ch = lvw1.ColumnHeaders.Add(10, "temp", "", 0)
2) add values from your two columns, combine like this: "AAAA_BBBB"
        ' for each row
             .SubItems(9) = .SubItems(7) & "_" & .SubItems(8)
3) sort listview by temporary column
        .SortOrder = lvwDescending
        .SortKey = 9
        .Sorted = True
4) remove temporary column
        lvw1.ColumnHeaders.Remove "temp"
0
Shiju SasidharanAssoc Project ManagerCommented:
Hi
try this
'----------------------------------------------------------
Private Sub ListView1_ColumnClick(Index As Integer, ByVal ColumnHeader As MSComctlLib.ColumnHeader)
    ListView1(Index).Sorted = True
    ListView1(Index).SortKey = ColumnHeader.SubItemIndex
    If ListView1(Index).SortOrder = lvwAscending Then
        ListView1(Index).SortOrder = lvwDescending
    Else
        ListView1(Index).SortOrder = lvwAscending
    End If
End Sub
'--------------------------------------------------------------

;-)
Shiju
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Shiju SasidharanAssoc Project ManagerCommented:
Hi

Sorry, ignore my previous post
consider this one

----------------------------------------------------------
Private Sub ListView1_ColumnClick( ByVal ColumnHeader As MSComctlLib.ColumnHeader)
    ListView1.Sorted = True
    ListView1.SortKey = ColumnHeader.SubItemIndex
    If ListView1.SortOrder = lvwAscending Then
        ListView1.SortOrder = lvwDescending
    Else
        ListView1.SortOrder = lvwAscending
    End If
End Sub
'--------------------------------------------------------------

;-)
Shiju


0
DabasCommented:
Hi nichia:
ameba beat me to the solution I would propose.
The hidden column is a good way to go.
It also works if you want to sort by a date column, but present it in a normal format.
By having a hidden column, with the data formatted to "yyyymmdd", you can overcome date sorting issues...

Dabas
0
nichiaAuthor Commented:
So far JR's solution works best for me, although I do not like the idea of sorting twice.

Ameba - I tried the temp column method, and it does sort properly. However, after I delete the column i get runtime errors while looping the Subitems.  I think that it thought the column data was still there, but the column header was deleted??
0
amebaCommented:
The column data will still be there, that's true.
If that is a problem, you can do a loop and remove it:
     For i = 1 To lvw1.Listitems.Count
         lvw1.ListItems(i).ListSubItems.Remove 9
     Next

But I suggest you change your loop which gives the error, to not include the last column.
0
nichiaAuthor Commented:
Thanks all!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

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.