How to order an array

I'm looking for the fastest code to sort an array storing prices getting the same as a database query ORDER BY ASC
neyAsked:
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.

eab111098Commented:
to optimize your "sort" code, i'd like you provide a little more background information. it just may be that by changing the ideology behind your code will solve your problem and give you optimum performance. if this is a simple array sorting problem, the easiest way to sort the array is to sort it while building it. but if you insist that an array is the way to go, then you can cheat and increase performance of any sorting algorithm you could write or get from here by simply using a listbox control and set its "sorted" property. if you don't want to have the listbox displayed, simply set its visible property to false. this is by far not the best way but, it is the easiest.

ed.
0
deightonprogCommented:
Best algorithm for array sorting is 'quick sort'.
0
mark2150Commented:
How many elements you need to sort? Fastest to *code* is use ListBox with .Sorted property True. If sort only happens occassionally in code, this is "fast enuf" with minimum effort/cost to implement.

M
0
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!

deightonprogCommented:
Here's a function to sort a 1-d array.  it needs amendments to sort 2d arrays (using columns like a db) based on a key column.  I've adapted it before though.

Its a quick algorithm, quick as well(.

Public Sub QuickSortNumbers(iArray As Variant, l As Long, r As Long)

    Dim i As Long, j As Long
   
    Dim x
    Dim y
   
    Form1.text1.Text = Form1.text1.Text + 1
    Form1.Refresh
   
    i = l
    j = r
   
    x = iArray((l + r) / 2)
   
    While i <= j
   
        While iArray(i) < x And i < r
       
            i = i + 1
           
        Wend
       
        While x < iArray(j) And j > l
       
            j = j - 1
           
        Wend
       
        If i <= j Then
       
            y = iArray(i)
            iArray(i) = iArray(j)
            iArray(j) = y
            i = i + 1
            j = j - 1
           
     
           
        End If
           
    Wend
   
    If l < j Then Call QuickSortNumbers(iArray, l, j)
    If i < r Then Call QuickSortNumbers(iArray, i, r)
   

End Sub

example to sort 50 numbers 1 - 50 in an array a(50)

Call QuickSortNumbers(a, 1, 50)



0
mark2150Commented:
Deighton, you need to "de-FORTRAN" your code! :-)

M
0
neyAuthor Commented:
I find the listbox shortcut impracticable as I loose the index of my original array. Let's say I have 10 units with different strenght, I fill the listbox and then set sort to true. Now the items are sorted but I can't say anymore which index had the first item in the list before being sorted.
Trying to be complete:
I use a user type var to store informations about units attribute
type Counter
..Id as integer
..Strenght as integer
..Fire as byte
.... ...
end type
Dim Unit(10) as Counter

Now I need a routine which get as input the Unit(10) and show as output another array UnitTemp(10) sorted by Strenght or any other attribute.

This would be immediate using a db table with columns labeled as strenght, Id, fire ... and "SELECT ... ORDER BY DESC" thus knowing the Id of the most power unit  but I'm trying to avoid to use the access engine.

I hope to have been enough clear.
0
mark2150Commented:
ney, you're incorrect when you say you can't track which item was which. You have the .ItemData(index) property that you can carry an associated piece of data along with the item. The associated data reads out in the same order the sorted list does so it workes perfectly. One limit is that the item is a numeric. If you want to keep track of a string as well as what you're sorting on then have a string array that you fill when you add the item to the list. You save the *index* into the string array as the value for the ItemData property and there you are. Easy. Fast enough for 99% of apps. Trivial to code.

M
0
neyAuthor Commented:
Thank you Mark2150, I only work with numeric items. How can I accept your answer. How can I share the points between you and eab who first answered my question?
0
mark2150Commented:
You can use the "Accept Comment as Answer" feature to award points. For a point split you have to contact customer service or kill this Q and repost a pair of Q's "For x Only"...

M
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
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.