SORTING A STRING ARRAY

This should be a no-brainer for seasoned VB people:

strMyArray() has three elements, containing "A", "Z", and "M".

Without writing my own sorting routine, how do I sort them, and, in this particular case, how do I sort them descending?



Dreaming_EagleAsked:
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.

mdouganCommented:
OK, you don't have to write your own sorting routine, I did it for you:

Public Sub SortArray(fList as variant)
Dim fSortedList() as String
Dim i as Long
Dim j as Long
Dim k as Long

if Not IsArray(fList) then Exit Sub

ReDim fSortedList(0) As String

fSortedList(0) = fList(0)
For i = 1 To UBound(fList)
    ReDim Preserve fSortedList(i) As String
    fSortedList(i) = fList(i)
    j = i - 1
    k = i
    Do While j > -1
       If fSortedList(k) > fSortedList(j) Then
          Exit Do
       Else
          Match = fSortedList(j)
          fSortedList(j) = fSortedList(k)
          fSortedList(k) = Match
          k = j
        End If
        j = j - 1
    Loop
Next i

Form i = 0 to UBound(fList)
    fList(i) = fSortedList(i)
Next i

Erase fSortedList
End Sub

You could call this :

Dim vArray as variant

vArray = Array("M", "C", "Z")
SortArray(vArray)
0
mdouganCommented:
Opps, if you wanted to allow for sorting in either direction, then you might want to include an optional parameter in the Sub:

Public Sub SortArray(fList as variant, Optional Desc as boolean)


Then, at the bottom of the routine, change it to read:

If Desc Then
   For i = 0 to UBound(fList)
       fList(UBound(fList) - i) = fSortedList(i)
   Next i
Else
   For i = 0 to UBound(fList)
       fList(i) = fSortedList(i)
   Next i
End If
Erase fSortedList
End Sub
0
Erick37Commented:
Option Explicit

Private Sub Command1_Click()
    Dim MyArray
    MyArray = Array("A", "Z", "M")
    Call SortDecending(MyArray)
    Debug.Print MyArray(0); MyArray(1); MyArray(2)
End Sub

Private Function SortDecending(a)
    Dim i, j As Long
    Dim tmp
    For i = 1 To UBound(a)
        For j = 0 To UBound(a)
            If LCase(a(i)) > LCase(a(j)) Then
                temp = a(j)
                a(j) = a(i)
                a(i) = temp
            End If
        Next
    Next
    SortDecending = a
End Function
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
The Ultimate Tool Kit for Technolgy Solution Provi

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 for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

mark2150Commented:
Simple sort for a few elements is trivial if you use the listbox control. Set it to sorted and read it out. You want descending, read from the bottom up. Simple to code and always works.

M
0
Dreaming_EagleAuthor Commented:
Ah, so there is not a sort array function...:)  I could have done this myself, but I did think I would have to.  However, Eric's routine was short, easy to implement, so I am giving him the points.  I wish I could split them between you, because they are both good answers...

thanks...
0
leesssCommented:
Due to lazyness, I use a sorted list box same as Mark2150's suggestion.

The list box is hidden, and I write a sub to wrap around it to sort.  The sub also accepts an optional asc or dsc as a boolean default to asc, to indicate asc or dsc order.  So I have a simple sub/function for sorting.
0
mdouganCommented:
I liked Erics routine better myself.  I'd cut and pasted from a routine I'd used where I didn't want to change the sort in the original array, so I created a separate sorted array.  But in this case it was unnecessary.

I wouldn't give up on a built in sort function yet, there might be one in VB 6, I'll look this afternoon.

0
Dreaming_EagleAuthor Commented:
I thought I had already done this...sorry, and thanks to all for the good suggestions.
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.