SortArray
SortArray Array(), StartElement, EndElement
 
Parameters:

    Array() = The Array you wish to sort
    StartElement = Starting element of range you wish sort
    EndElement = End element of the range you wish to sort
Returns: NONE
 

      SortArray will sort an array of Integer / Float values or Strings into ascending (alphabetical) order. The StartElement and EndElement let us control what section of the array is sorted. So you don't need to sort the entire thing each time.



FACTS:


      * SortArray only supports Integer, Float & String arrays. Type arrays are not currently supported.

      * SortArray Only supports 1D arrays.

      * Values will be sorted into ascending order. (lowest to highest)

      * Strings will be sorted into alphabetical. (A-Z) - Case Is not important, so a string aabb and AABB would have equal sort precedence

      * SortArray uses the none comparative custom algorithm we call 'freq sorting' . This method gives very good results on [integer / float and string arrays. Older editions of PlayBASIC uses to use Quick Sort, but this much quicker.





Mini Tutorial #1:


      This example with create an Integer Array, then fill it full random values, sort the array and then display the contents.

[code]
; Create an Array...
Dim Table(10)

; Fill theTable() Array with random values between 0 and 100.
For lp =0 to 10
Table(lp)=rnd(100)
next

; Display the array before it's sorted.
Print "The Array Before Being Sorted"
For lp =0 to 10
print Table(lp)
next

; Sort the table array into ascending order using SortArray.
; This will sort the elements between array index 0 and
; array index 10.
SortArray Table(),0,10

; Display the sorted table() array
Print "The Array After It's Been Sorted"
For lp =0 to 10
print Table(lp)
next

; Show the Display and wait for a key press
Sync
Waitkey
[/code]


      The output of this code should look something like this. (although the table will be filled with random values each time.. So i'll just make up the values for the sake of this example.)

The values before the array is sorted.

  
  45
  18
  74
  2
  53
  99
  6
  88
  23
  34
  72
  


The array values after being sorted.

  
  2
  6
  18
  23
  34
  45
  53
  72
  74
  88
  99
  





Mini Tutorial #2:


      In this example we're creating a array of names and using SortArray to sort them into alphabetical order.


  
  Size=10
  Dim name$(Size)
  For lp=0 To size
     name$(lp)=ReadData$()
  Next
  
  
  SortArray name$(),0,size
  
  For lp=0 To size
     Print name$(lp)
  Next
  Sync
  WaitKey
  
  Data "zack","wolf","kev","andy","michelle"
  Data "dogs","tess","c64","zynaps","dudes","Alpha"
  
  






 
Example Source: Download This Example
; ========================================
; SortArray EXAMPLE
; ========================================
  
; Create an Integer Array called "MyArray" containing a random number of elements
  Dim MyArray(RndRange(1,20))
  
; Fill MyArray() With random Values
  FillMyarray()
  
  Repeat
   ;Clear the Screen To Black
     Cls RGB(0,0,0)
     
   ; Display the Contents Of the Two Arrays.
     Print "Array Contents"
     ShowMyArray("Myarray()",Myarray())
     
   ; Display Some Controls So The user can Experiment with the
     Print ""
     Print "KeyBoard Controls"
     Print "================="
     Print "       F = Fill MyArray() with random values"
     Print "       S = Sort Myarray() into ascending order"
     Print "   Space = Exit"
     
   ; Check if the user presses a key.
     Select Lower$(Inkey$())
         Case "f"
           ; If the user pressed the "F" key Then fill the  Myarray() with random values.
             FillMyArray()
         Case "s"
           ; If the user pressed the "s" key Then Sort the Myarray()
             SortArray Myarray(),0,GetArrayElements(Myarray(),1)
     EndSelect
     
   ; Draw the Screen
     Sync
   ; Repeat Until The SpaceKey has been pressed
  Until SpaceKey()
  
; END The Program
  End
  
  
  
Function ShowMyArray(Message$,ThisArray())
; Compile the contents of our the MyArray into a String And display them.
  Print ""
  Print Message$
  Print Make$("-",Len(message$))
  If GetArrayStatus(thisArray())=true
     size=GetArrayElements(ThisArray(),1)
     For lp =0 To size
        Contents$=Contents$+Digits$(ThisArray(lp),3)
        If lp<size Then Contents$=Contents$+","
     Next
  Else
     Contents$="Array Is Not Defined"
  EndIf
  Print Contents$
  Print ""
EndFunction
  
  
Function  FillMyArray()
; Fill MyArray() with Random values between 0 And 1000
  If GetArrayStatus(MyArray())=true
     For lp=0 To GetArrayElements(MyArray(),1)
        MyArray(lp)=Rnd(1000)
     Next
  EndIf
  
  EndFunctio
 
Related Info: CopyArray | CopyArrayCells | Dim | SearchHighestArrayCell | SearchLowestArrayCell :
 


(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com