B4R Question sorting an array

peacemaker

Expert
Licensed User
Longtime User
Hi, All

If to read from a sensor several times to get AVG value, i'd like to skip some min and some max values.
Any built-in function to sort arrays in B4R ?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Sub Process_Globals
    Public Serial1 As Serial
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    Dim p As Pin
    p.Initialize(0, p.MODE_INPUT)
    RndSeed(p.AnalogRead)
    Dim values(10) As Double
    Log("before:")
    For i = 0 To values.Length - 1
        values(i) = Rnd(0, 100000) / 1000
        Log(values(i))
    Next
    SortArray(values)
    Log("after:")
    For i = 0 To values.Length - 1
        Log(values(i))
    Next
    
End Sub

'Bubble sort
Sub SortArray(Values() As Double)
    Dim temp As Double
    Dim sorted As Boolean = False
    Do While sorted = False
        sorted = True
        For i = 0 To Values.Length - 2
            If Values(i) > Values(i + 1) Then
                temp = Values(i)
                Values(i) = Values(i + 1)        
                Values(i + 1) = temp
                sorted = False
            End If
        Next
    Loop
End Sub
 
Upvote 0
Top