LearningCharlito
LearningCharlito

Reputation: 368

Kotin sort by descending then ascending

Im trying to order a list on multiple parameters.. for example, one value descending, second value ascending, third value descending.

 var people = listOf<People>(People("palazo", "ooo", 1),
            People("asdf", "cccc", 2),
            People("Maria", "ooo", 3),
            People("Helena", "ccccc", 3),
            People("Carlos", "ccc", 4),
            People("Jhon", "ooo", 2)
    )

is there a way like this to do it? (i know is incorrect) people = people.sortedByDescending { it.name}.thenBy{it.lastname}.thenDescending{it.age}

or add to :

   people.sortedWith(compareBy(People::name, People::lastName)) 
   //but mixing ascending and descending

Upvotes: 0

Views: 1111

Answers (1)

IR42
IR42

Reputation: 9682

val sortedPeople = people.sortedWith(
    compareByDescending(People::name)
        .thenBy(People::lastName)
        .thenByDescending(People::age)
)

Upvotes: 1

Related Questions