I forgot.. can I sort array?
Local arr:Int[] = [12, 3, 4, 2, 4, 1, 1, 4, 8]
Sort(arr)
I forgot.. can I sort array?
Local arr:Int[] = [12, 3, 4, 2, 4, 1, 1, 4, 8]
Sort(arr)
I think not directly. You can put it in a IntList and then use the sort method. There was a user module for this but the link I found was dead.
Thank Phil. btw... for some who need this for sorting array 1 dim
Function Sort:Void(arr:Int[],ascending:Bool=True)
Local n:Int = arr.Length()
For Local i:Int = 0 Until n-1
For Local j:Int = 0 Until n-i-1
If ascending
If arr[j] < arr[j+1]
Local temp:Int = arr[j]
arr[j] = arr[j+1]
arr[j+1] = temp
End If
Else
If arr[j] > arr[j+1]
Local temp:Int = arr[j]
arr[j] = arr[j+1]
arr[j+1] = temp
End If
endif
Next
Next
End
Display More
That code seem like Bubble Sort like sorting things in order or Football league table....
You can do Quick sort like this
' Quicksort
Strict
Import mojo2
Function Main:Int()
New MYGame
Return 0
End
Class MYGame Extends App
Field canvas:Canvas
Method OnCreate:Int()
canvas=New Canvas()
SetSwapInterval 1 ; SetUpdateRate 0
' Example
Local arr:Int[] = [5, 3, 8, 4, 2, 7, 1, 6]
arr = QuickSort(arr, 0, arr.Length - 1)
Print "Sorted array:"
For Local i:Int = 0 Until arr.Length
Print arr[i]
Next
Return 0
End
Method OnUpdate:Int()
If KeyHit(KEY_ESCAPE) Then EndApp
Return 0
End
Method OnRender:Int()
canvas.Clear
canvas.Flush
Return 0
End
End
Function QuickSort:Int[] (arr:Int[], low:Int, high:Int)
If low < high
Local temp:Int
Local pivot:Int = arr[high], i:Int = low - 1
For Local j:Int = low Until high
If arr[j] <= pivot
i = i + 1
temp = arr[i] ; arr[i] = arr[j] ; arr[j] = temp
End If
Next
temp = arr[i + 1] ; arr[i + 1] = arr[high] ; arr[high] = temp
Local pi :Int = i + 1
QuickSort(arr, low, pi - 1) ; QuickSort(arr, pi + 1, high)
End If
Return arr
End Function
Display More
Here's my hack for sorting anything according to a value: https://www.cerberus-x.com/community/inde…-new-class.159/
It would be a bit of overhead calculation-wise, but:
Local sorter := New SortItem< Int >()
For Local item:Int = Eachin arr
sorter.Add( item, item )
Next
arr = sorter.Sort().ToArray()
sorter.Add( item, item )
The example in the link shows the value of your code much better, because if the array already is of a sortable type it is easier to use a regular IntList, FloatList or StringList.
But whenever you want to sort objects that are not sortable this is really handy. For example if you have something like:
sorter.Add(player, player.score/player.timePlayed) to get a sorted list of the most effective players.
Here's my hack for sorting anything according to a value: https://www.cerberus-x.com/community/inde…-new-class.159/
You are using the actual featurs of the language to make data general enough to sort it whatever it is, If I understand it right, that's impressive.
The example in the link shows the value of your code much better, because if the array already is of a sortable type it is easier to use a regular IntList, FloatList or StringList.But whenever you want to sort objects that are not sortable this is really handy. For example if you have something like:
sorter.Add(player, player.score/player.timePlayed) to get a sorted list of the most effective players.
Except that a StringList sorts by name... my example sorts strings by length instead! [Actually my code wouldn't be great for sorting strings the usual way, as it requires the sort key to resolve to a float. But that's what you want in most use cases.]
Don’t have an account yet? Register yourself now and be a part of our community!