Solved

Sorting an Array of Strings

Posted on 2002-05-17
9
333 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

717 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