Solved

Sorting an Array of Strings

Posted on 2002-05-17
9
330 Views
Last Modified: 2010-05-02
I need to sort an array of strings - the fastest way possible.

I've tried bubblesort, but it's too slow.

Can someone send me some decent code which I can use.

A little explanation of what's happening would be great too,

thanks,

Davey.
0
Comment
Question by:DaveyByrne
9 Comments
 
LVL 14

Expert Comment

by:ajexpert
ID: 7015752
Well I think you could populate the listbox with the array you have.  Make sure to make sort propery of Listbox to True.  Make the listbox hidden.

The snippet is given

    Dim arr1(4) As String  ' Given Array
    Dim arr2(4) As String  ' Sorted Array
   

        arr1(0) = "M"
        arr1(1) = "G"
        arr1(2) = "H"
        arr1(3) = "S"
        arr1(4) = "A"
   

    ' Adding array to listbox
    For i = 0 To UBound(arr1)
        List1.AddItem arr1(i)
    Next i
   
    ' Getting the sorted array
    For i = 0 To (List1.ListCount - 1)
        arr2(i) = List1.List(i)
    Next i
   
    For i = 0 To UBound(arr2)
        Msgbox arr2(i)
    Next i
0
 
LVL 1

Accepted Solution

by:
falconew earned 100 total points
ID: 7015791
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 7015902
Beside of arrays, have you tried :

. an invisible comboobx (setting the Sorted property to True only when all the data have been loaded)

. an in-memory ADO recordset that can be sorted too.
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 26

Expert Comment

by:EDDYKT
ID: 7015982
How many string are you going to sort.
additem for listbox is slow compare to use listview
0
 
LVL 4

Expert Comment

by:gencross
ID: 7016113
Here is function I found.  I have not tried it.

  InsertSortAscending
 
 Print  
  Email  

Function InsertSortAscending(ValueList As Variant) As Variant
    '#######################################
    '     ##############################
    ' InsertSortAscending
    '
    ' This function recieves a single dimens
    '     ional array of any datatype
    ' that is not an object, and returns an
    '     array which has been sorted
    ' in ascending order. It uses a standard
    '     insert sort algorithm without
    ' data partitioning. It is relatively fa
    '     st, and can sort 1000 4-digit
    ' integers in 1 second, and 1000 5-chara
    '     cter strings in 1 second when
    ' in P-Code.
    '
    ' Usage:
    'Dim ArrayToBeSorted() as string
    'Dim SortedArray as Variant
    'SortedArray = InsertSortAscending(Array
    '     ToBeSorted)
    '
    ' Written by: Andy Carrasco (gabrielw@ix
    '     .netcom.com) 3/3/97
    '#######################################
    '     ##############################
    Dim RipVal As Variant
    Dim RipOrdinal As Long
    Dim RipDescent As Long
    Dim PrivateBuffer As Variant
    Dim Placed As Boolean
    Dim X As Long
    Dim Y As Long
    PrivateBuffer = ValueList
    'Ok, we start at the second position in
    '     the array and go
    'from there
    RipOrdinal = 1
    RipDescent = 1


    For Y = 1 To UBound(PrivateBuffer)
        RipVal = PrivateBuffer(Y)
        If Y <> 1 Then RipDescent = Y


        Do Until Placed


            If PrivateBuffer(RipDescent - 1) >= RipVal Then
                RipDescent = RipDescent - 1


                If RipDescent = 0 Then


                    For X = Y To RipDescent Step -1
                        If X = 0 Then Exit For
                        PrivateBuffer(X) = PrivateBuffer(X - 1)
                    Next X
                    PrivateBuffer(RipDescent) = RipVal
                    Placed = True
                End If
            Else
                'shift the array to the right


                For X = Y To RipDescent Step -1
                    If X = 0 Then Exit For
                    PrivateBuffer(X) = PrivateBuffer(X - 1)
                Next X
                'insert the ripped value
                PrivateBuffer(RipDescent) = RipVal
                Placed = True
            End If
        Loop
        Placed = False
    Next Y
 
 
 
0
 
LVL 4

Expert Comment

by:nutwiss
ID: 7016752
as falconew saiys, thge quicksort algorithm would be ideal as long as there are only a few (less than a thousand or so) strings.
There are many, many types of sort algorithm, have a look on the net, you might have to re-write something from another language tho'
0
 
LVL 4

Expert Comment

by:nutwiss
ID: 7016754
PS, as Alexpert says - I have been known to use a hidden listbox in the past......disgusting habit, but it's remarkably quick... :)
0
 
LVL 2

Expert Comment

by:mirtol
ID: 7016963
I had forgotten how offensive VB was... hidden list boxes for sorting strings? I don't know.

The reason why such an absurd method may well be quicker than the quicksort algorithm (which is by far the best) is because you're swapping strings around all over the place.

Oh it is so simple in C. You just swap your pointers around :o) (which is, by the way, what the listbox will do)

In VB, create an array of indeces and sort the indeces using the quicksort method.

Ie:

Dim MyStrings(1 to 100) as string
Dim i(1 to 100) as integer

For n%=1 to 100
    i(n%) = n%
Next

[Implement quicksort method, but refer to strings by MyStrings(i(INDEX)) and when you have to swap two strings in the algorithm - just swap the i(INDEX) numbers]

0
 
LVL 1

Author Comment

by:DaveyByrne
ID: 7021912
Since you were the first to give me the code, and I ended up using it - you've got the points.

Thanks to everyone for their answers,

Dave.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

786 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