Android Code Snippet Sort Array of Int

SubName: It´s more an how to do as an Sub

Description: How to sort an array of INTs and get the Top 5 of them

In this code the TOP5 of the given Int-Array is evaluated

B4X:
Dim A() As Int
A = Array As Int(5, 7, 1, 4, 9, 2)
Dim L As List
L.Initialize
L.AddAll(A)
L.Sort(False)
For i = 0 To 4
  Log(L.Get(i))
Next

9
7
5
4
2

Tags: sort array
 

eps

Expert
Licensed User
Longtime User
Forgive me, Speedy, but so you do not get a sorted array.

but you could edit the post, creating a real function by copying the list in the array

?

This

L.Sort(False)

Will sort the Array...

or are you asking something different?
 

LucaMs

Expert
Licensed User
Longtime User
It sorts the List, not the Array A
B4X:
Dim A() As Int
A = Array As Int(5, 7, 1, 4, 9, 2)
Dim L As List
L.Initialize
L.AddAll(A)
L.Sort(False)
For i = 0 To 4
  Log(L.Get(i))
Next
Log("....")
For i = 0 To 4
  Log(A(i))
Next
 

LucaMs

Expert
Licensed User
Longtime User
It could be:
B4X:
Public Sub SortArray(Arr() As Int, Ascending As Boolean)
    Dim L As List
    L.Initialize
    L.AddAll(Arr)
    L.Sort(Ascending)
    For i = 0 To L.Size - 1
        Arr(i) = L.Get(i)
    Next
End Sub

Dim A() As Int
A = Array As Int(5, 7, 1, 4, 9, 2)

Log("Unsorted")
For i = 0 To 4
  Log(A(i))
Next

Log("Ascending")
SortArray(A, True)
For i = 0 To 4
  Log(A(i))
Next

Log("Descending")
SortArray(A, False)
For i = 0 To 4
  Log(A(i))
Next

You could probably adapt to any type of array, passing an object variable as a parameter and using my snippet about the data type of the array
 
Last edited:

LucaMs

Expert
Licensed User
Longtime User
This is not bad. It should be optimized if possible.
It uses: ArrayType.
I do not know why it does not work with Char (and I do not want to know )

B4X:
Public Sub SortArray(ArrayObject As Object, Ascending As Boolean)

    Dim L As List
    L.Initialize

    Dim ArrayDataType As String = ArrayType(ArrayObject)

    Select Case ArrayDataType
        Case "Byte"
            Dim arrByte() As Byte
            arrByte = ArrayObject
            L.AddAll(arrByte)
            L.Sort(Ascending)
            For i = 0 To L.Size - 1
                arrByte(i) = L.Get(i)
            Next
            ArrayObject = arrByte
        Case "Short"
            Dim arrShort() As Short
            arrShort = ArrayObject
            L.AddAll(arrShort)
            L.Sort(Ascending)
            For i = 0 To L.Size - 1
                arrShort(i) = L.Get(i)
            Next
            ArrayObject = arrShort
        Case "Int"
            Dim arrInt() As Int
            arrInt = ArrayObject
            L.AddAll(arrInt)
            L.Sort(Ascending)
            For i = 0 To L.Size - 1
                arrInt(i) = L.Get(i)
            Next
            ArrayObject = arrInt
        Case "Long"
            Dim arrLong() As Long
            arrLong = ArrayObject
            L.AddAll(arrLong)
            L.Sort(Ascending)
            For i = 0 To L.Size - 1
                arrLong(i) = L.Get(i)
            Next
            ArrayObject = arrLong
        Case "Float"
            Dim arrFloat() As Float
            arrFloat = ArrayObject
            L.AddAll(arrFloat)
            L.Sort(Ascending)
            For i = 0 To L.Size - 1
                arrFloat(i) = L.Get(i)
            Next
            ArrayObject = arrFloat
        Case "Double"
            Dim arrDouble() As Double
            arrDouble = ArrayObject
            L.AddAll(arrFloat)
            L.Sort(Ascending)
            For i = 0 To L.Size - 1
                arrDouble(i) = L.Get(i)
            Next
            ArrayObject = arrDouble
        Case "Char" ' don't works
'            Dim arrChar() As Char
'            arrChar = ArrayObject
'            L.AddAll(arrChar) ' gets an error
'            L.Sort(Ascending)
'            For i = 0 To L.Size - 1
'                arrChar(i) = L.Get(i)
'            Next
'            ArrayObject = arrChar
        Case "String"
            Dim arrString() As String
            arrString = ArrayObject
            L.AddAll(arrString)
            L.Sort(Ascending)
            For i = 0 To L.Size - 1
                arrString(i) = L.Get(i)
            Next
            ArrayObject = arrString
    End Select

End Sub


Example:
B4X:
Dim FloatArray() As Float
FloatArray = Array As Float(5.1, 7.2, 1.3, 4.4, 9.5, 2.6)

Dim StringArray() As String
StringArray = Array As String("Klaus", "Erel", "DonManfred", "Stevel05", "Thedesolatesoul", "Agraham")

Log("Unsorted")
For i = 0 To FloatArray.Length - 1
  Log(FloatArray(i))
Next
For i = 0 To FloatArray.Length - 1
  Log(StringArray(i))
Next

Log("___________________")

Log("Ascending")
SortArray(FloatArray, True)
For i = 0 To 4
  Log(FloatArray(i))
Next
Log("")
SortArray(StringArray, True)
For i = 0 To FloatArray.Length - 1
  Log(StringArray(i))
Next

Log("___________________")

Log("Descending")
SortArray(FloatArray, False)
For i = 0 To 4
  Log(FloatArray(i))
Next
Log("")
SortArray(StringArray, False)
For i = 0 To FloatArray.Length - 1
  Log(StringArray(i))
Next

Log:
Unsorted
5.099999904632568
7.199999809265137
1.2999999523162842
4.400000095367432
9.5
2.5999999046325684
Klaus
Erel
DonManfred
Stevel05
Thedesolatesoul
Agraham
___________________
Ascending
1.2999999523162842
2.5999999046325684
4.400000095367432
5.099999904632568
7.199999809265137
Agraham
DonManfred
Erel
Klaus
Stevel05
Thedesolatesoul
___________________
Descending
9.5
7.199999809265137
5.099999904632568
4.400000095367432
2.5999999046325684
Thedesolatesoul
Stevel05
Klaus
Erel
DonManfred
Agraham
 
Last edited:

wonder

Expert
Licensed User
Longtime User
If you really want to sort an array, and not a list populated with an array (which is a very very slow method), you can find the code in the french forum here.

More optimization is always welcome. Could you summarize the post in English and re-post the code? Thanks in advance.
 

LucaMs

Expert
Licensed User
Longtime User
I'm sure that an automatic translator will do the job very well.

I have not read that post (not because it is in French, but because I'm lazy ).
Probably there will be sorting algorithms, like QuickSort, ShellSort, BubbleSort...
Possibly you could replicate the link (only) in the forum Snippets.
This task belongs to the author, of course


[automatic translation]
Je ne l'ai pas lire le post (non pas parce qu'il est en français, mais la paresse).
Il y aura probablement des algorithmes de tri, comme QuickSort, ShellSort, BubbleSort ...
Peut-être que vous pourriez reproduire le seul lien dans les Snippets forum.
Cette tâche appartient à l'auteur, bien sûr.

uhm... not so good... "mais la paresse"??? je suis paresseux!