Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting Items in a ListBox appears to sort Proper Case words then lower case words 1

Status
Not open for further replies.

PWD

Technical User
Jul 12, 2002
823
GB
Good afternoon, I obtained the following code today to sort the items populated in a ListBox:-

Code:
 'Sort by the 1st column in the ListBox Alphabetically in Ascending Order
Run "SortListBox", UserForm2.ListBox1, 0, 1, 1

Sub SortListBox(oLb As MSForms.ListBox, sCol As Integer, sType As Integer, sDir As Integer)
    Dim vaItems As Variant
    Dim i As Long, j As Long
    Dim c As Integer
    Dim vTemp As Variant
     
     'Put the items in a variant array
    vaItems = oLb.List
     
     'Sort the Array Alphabetically(1)
    If sType = 1 Then
        For i = LBound(vaItems, 1) To UBound(vaItems, 1) - 1
            For j = i + 1 To UBound(vaItems, 1)
                 'Sort Ascending (1)
                If sDir = 1 Then
                    If vaItems(i, sCol) > vaItems(j, sCol) Then
                        For c = 0 To oLb.ColumnCount - 1 'Allows sorting of multi-column ListBoxes
                            vTemp = vaItems(i, c)
                            vaItems(i, c) = vaItems(j, c)
                            vaItems(j, c) = vTemp
                        Next c
                    End If

.
.
.
     'Set the list to the array
    oLb.List = vaItems
End Sub

But it appears to be sorting the items that begin with a Capital Letter then sorting those that don't, e.g. C, F, J & Q followed by j & m.

Bearing in mind that I had to go & find this code in the first place, can anyone suggest, a) the reason for this behaviour & b) something to make it work the way that I think it should work?

Many thanks,
D€$
 
That's because "A" is not of the same value as "a".
"A" is 0041 while "a" is 0061.

If you want case-insensitive sorting, change this line:
Code:
If vaItems(i, sCol) > vaItems(j, sCol) Then
to this:
Code:
If [b]LCase[/b](vaItems(i, sCol)) > [b]LCase[/b](vaItems(j, sCol)) Then

That should do the trick.

Cheers,
MakeItSo

“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
By jove, that did it. Thanx.

Many thanks,
D€$
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top