Home > database >  Find and move specific item at the top of list
Find and move specific item at the top of list

Time:09-28

I have the string let's assume "lorem".

From Api I download list with items and one of them contains "lorem" as a field (title).

How I can move that item at the top of the list and don't change positions of other items?

 list = downloadedData.items?.map {
                        Item(
                            text = it.title,
                            onClick = Button { }
                        )
                    }

CodePudding user response:

For mutable list: first search position of lorem then use removeAt() to remove that item. Then add searched item at beginning

let item is your searched item

list.removeAt(position)
        list.add(0,item)
  • Related