Android Question Reorder elements in list

epicsapis

Member
I have a list of elements and I need to do some elements reordering without moving the element in to top (because that would have an undesirable visual effect), so, I can't use `ele.bringToFront` at any time.

Basically I need the ability to switch the position of two element without moving the others.
Example:

Switch the order of two elements:
'My List
Dim elems As List = ["Apple", "Boy", "Car", "Daniel", "Enter"]

'The function I need
elems = switchOrder(elems, 1, 4)

'Output
'["Apple", "Enter", "Car", "Daniel", "Boy"]

Also, I need the ability to reverse the order of all the elements except the three of them. Example:

Switch the order of two elements:
'My List
Dim elems As List = ["Apple", "Boy", "Car", "Daniel", "Enter", "Fast", "Gas", "Hot"]

'The function I need
elems = reverseExcept(elems, 0, 1, 7)

'Output
'["Apple", "Boy", "Gas", "Fast", "Enter", "Daniel", "Car", "Hot"]
'Note that "Enter" didn't change the position because there's an odd number of reorder elements
'in the case of an even number of elements, all the elements (except the the index 0, 1 and 7) should change
 

pliroforikos

Active Member
Licensed User
I haven't checked

B4X:
Sub ReverseExcept(Elems As List, Index1 As Int, Index2 As Int, Index3 As Int)
    Dim elems2 As List
    elems2.Initialize
 
    For i = 0 To Elems.Size-1
        If i <> Index1 And i <> Index2 And i <> Index3 Then
            elems2.Add(Elems.Get(i))
        End If
    Next
 
    For i = 0 To (elems2.Size-1)/2
        SwitchOrder(elems2, i, (elems2.Size-1) - i)  
    Next
 
    Dim j As Int = 0
    For i = 0 To Elems.Size-1
        If i <> Index1 And i <> Index2 And i <> Index3 Then
             Elems.Set(i, Elems2.Get(j)
            j = j + 1
        End If
    Next
 
End Sub
 
Last edited:
Upvote 0
Top