Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 339
  • Last Modified:

Sorting an Array of Strings

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
DaveyByrne
Asked:
DaveyByrne
1 Solution
 
ajexpertCommented:
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
 
falconewCommented:
0
 
Éric MoreauSenior .Net ConsultantCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
EDDYKTCommented:
How many string are you going to sort.
additem for listbox is slow compare to use listview
0
 
gencrossCommented:
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
 
nutwissCommented:
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
 
nutwissCommented:
PS, as Alexpert says - I have been known to use a hidden listbox in the past......disgusting habit, but it's remarkably quick... :)
0
 
mirtolCommented:
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
 
DaveyByrneAuthor Commented:
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

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now