[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

SORTING A STRING ARRAY

Posted on 1999-11-18
8
Medium Priority
?
132 Views
Last Modified: 2010-05-02
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?



0
Comment
Question by:Dreaming_Eagle
8 Comments
 
LVL 18

Expert Comment

by:mdougan
ID: 2216769
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
 
LVL 18

Expert Comment

by:mdougan
ID: 2216780
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
 
LVL 32

Accepted Solution

by:
Erick37 earned 150 total points
ID: 2216805
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 12

Expert Comment

by:mark2150
ID: 2217183
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
 

Author Comment

by:Dreaming_Eagle
ID: 2217345
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
 
LVL 1

Expert Comment

by:leesss
ID: 2217457
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
 
LVL 18

Expert Comment

by:mdougan
ID: 2217511
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
 

Author Comment

by:Dreaming_Eagle
ID: 2230136
I thought I had already done this...sorry, and thanks to all for the good suggestions.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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 process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

590 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