Vivek Modi
Vivek Modi

Reputation: 7151

Filter last element and add to top of list in mutable list kotlin

I am new in Kotlin. I need your help to sort my mutableList in custom data class. I need to find the search element in the list and put into the top of list. Mostly the search element is in the last element. So I don't know how to filter that. Please give me some suggestions how to achieve that. For example

Data Class

data class Person(val firstName: String, val lastName: String) 

data class Item(val gender: Int, val human: List<Human>)

data class Human(val id: Int, val person: List<Person>)

I entered some fake data

val people = mutableListOf(
        Item(1,
                listOf(
                        Human(
                                1,
                                listOf(
                                        Person("Ragnar", "Lodbrok"),
                                        Person("Bjorn", "Ironside"),
                                        Person("Sweyn", "Forkbeard")
                                )
                        ),
                        Human(
                                2,
                                listOf(
                                        Person("Ragnar", "Lodbrok"),
                                        Person("Bjorn", "Ironside"),
                                        Person("Sweyn", "Forkbeard")
                                )
                        )

                )
        )
)

If i want to search Forkbeard and want to put in top of list. But i am unable to do this. So please suggest me some good advice.

I tried this but not working

people.forEach { people ->
        people.human.forEach { human ->
            human.person.sortedByDescending { person ->
                person.lastName == "Forkbeard"
            }
        }
    }

I am getting this

[Item(gender=1, human=[Human(id=1, person=[Person(firstName=Ragnar, lastName=Lodbrok), Person(firstName=Bjorn, lastName=Ironside), Person(firstName=Sweyn, lastName=Forkbeard)]), Human(id=2, person=[Person(firstName=Ragnar, lastName=Lodbrok), Person(firstName=Bjorn, lastName=Ironside), Person(firstName=Sweyn, lastName=Forkbeard)])])]

Answer

I want this

[Item(gender=1, human=[Human(id=1, person=[Person(firstName=Sweyn, lastName=Forkbeard),Person(firstName=Ragnar, lastName=Lodbrok), Person(firstName=Bjorn, lastName=Ironside)]), Human(id=2, person=[Person(firstName=Sweyn, lastName=Forkbeard),Person(firstName=Ragnar, lastName=Lodbrok), Person(firstName=Bjorn, lastName=Ironside)])])]
[ Sweyn Forkbeard, Ragnar Lodbrok, Bjorn Ironside ]

Thanks a lot

Upvotes: 1

Views: 2012

Answers (2)

Emmanuel Guerra
Emmanuel Guerra

Reputation: 1624

To get a single item from a list, you can use the first function:

val sweyn: Person = people.first { it.lastName == "Forkbeard" }

Then, for adding sweyn at the top:

people.remove(sweyn)
people.add(0, sweyn)

If you need to constantly do this, maybe you are looking for a Queue

EDIT:

For the nested human list, you can do the same if you change the person's type to MutableList in the Human data class:

people.forEach { people ->
    people.human.forEach { human ->
        val sweyn: Person = human.person.first { it.lastName == "Forkbeard" }
        human.person.remove(sweyn)
        human.person.add(0, sweyn)
    }
}

Upvotes: 0

Trevor
Trevor

Reputation: 1451

A trick to do all of this in just one line in Kotlin is with sortBy

people.sortByDescending { it.lastName == "Forkbeard" }

Upvotes: 3

Related Questions