# Thread: Sorting arrays with built-in sorting functions

1. ## Sorting arrays with built-in sorting functions

Sorting numeric arrays?
Using built-in sorting functions it can take ... no time.

``` '---Globals
DIM MaxCount     AS LONG value 100000
dim MyArray(MaxCount) as long
dim Count      as long
dim T1, T2      as double
DIM Message     AS STRING

Message = "This program will fill an array of " & MaxCount & " elements with random LONG numbers.\n"
Message += "Array will be sorted twice in order to test sorting of sparse and already sorted array.\n\n"
Message += "Please press Yes to go on, NO to Stop\n"
DIM lResult AS LONG = MSGBOX(0, Message, %MB_YESNO, "Continue?")
IF lResult <> %IDYES THEN
STOP
END IF

'---Speed up operations a bit
doevents(off)

'---Init random number generator
randomize

'---Fill array with random numbers
T1 = GetTickCount
for Count = LBound(MyArray) To UBound(MyArray)
MyArray(Count) = rnd(-1000000000, 1000000000)
next
T2 = GetTickCount

'---Do the job
msgbox 0, _
"Time to fill randomly an array of " & ubound(MyArray) & " elements:" & \$tab & format\$(T2-T1) & " mSecs" & \$crlf & _
"Time to sort array "               & repeat\$(5, \$tab) & testsort(MyArray)     & " mSecs" & \$crlf & _
"Time to re-sort sorted array "          & repeat\$(4, \$tab) & testsort(MyArray)     & " mSecs" & \$crlf & _
"Time to re-sort sorted array in descending order " & repeat\$(2, \$tab) & testsort(MyArray, %TRUE) & " mSecs" & \$crlf & _
""

'----------------------------------------------------------------------------
'---In case you want to see some output values, just uncomment those lines
'----------------------------------------------------------------------------
'uses "console"
'for count = 1 to 80
' console_write MyArray(Count) & ", "
'next
'console_waitkey

'----------------------------------------------------------------------------
'---Sorting function
'----------------------------------------------------------------------------
Function TestSort(byref v() as long, optional DeScendSorting as long) as long

'---Time measuring vars
local ticka, tickb as double

'---Start timer
ticka = GetTickCount

'---Sort array passed by reference depending on sort order
if DeScendSorting = %TRUE then
array sort v, descend
else
array sort v
end if

'---ENd timer
tickb = GetTickCount
function = tickb - ticka

End Function
```

2. ## Re: Sorting arrays with built-in sorting functions

Awesome we need these kind of samples Eros, thanks so much. You might want to talk with Roberto, he had an interesting idea of placing all of these in a repository so easy to find.

3. ## Re: Sorting arrays with built-in sorting functions

Repository is already there at http://scripts.thinbasic.com/ but abandoned.
I did it almost one year ago but at that time it was not the right time.

I will consider your wiki suggestion checking which is the best around.
I want it easy to be used and with syntax highlight.

Ciao
Eros

4. ## Re: Sorting arrays with built-in sorting functions

Bookmarked, never saw it or don't remember that. I would sticky that to the top or put in the menubar on the forums for sure. Great!!

5. ## Re: Sorting arrays with built-in sorting functions

Ken,

we are not maintaining it anymore.
I will delete it and substitute with a new one after choosing the best we can find.
So continue your suggestion about wiki. I will follow it and in the meantime try to find what will be better for thinBasic community.

Ciao
Eros

6. Have you considered using a wiki for maintaining and sharing these code samples for the thinBasic community, as suggested by kryton9?

7. Originally Posted by Limaxdum
Have you considered using a wiki for maintaining and sharing these code samples for the thinBasic community, as suggested by kryton9?
Those messages posted by AI bot are doing great job