1

Current scenario,

I have 3 arraylist

name = [a,b,c,a]
age = [2,3,5,6]
score = [3,4,4,1]

As an outcome I want

name = [b,c,a,a]
age = [3,5,2,6]
score = [4,4,3,1]

I figured that using a map would be useful but I got confused after doing score.map{v -> index} :|

3
  • Convert your three separate lists into a single list of some new object (that has name, age, and score properties) and then sort that list. You can then convert that sorted list of objects back to three separate lists if you want. Commented Jul 10, 2020 at 17:41
  • Wouldn't it increase required space and time? @BenP. Commented Jul 10, 2020 at 17:48
  • Theoretically, but it would be negligible unless these lists have hundreds of thousands of items in them. Commented Jul 10, 2020 at 18:15

1 Answer 1

2

I assume that your values all belong to each other like in a table, for example:

        | name | age | score
------------------------------
index 0 |  a   |  2  |   3
index 1 |  b   |  3  |   4
index 2 |  c   |  5  |   4
index 3 |  a   |  6  |   1

and you want to sort it by the score column.

There are multiple approaches to solve this issue, so I will show my favorite one below.

Using a data class

Declare the data class

For this purpose, it would be good to declare a simple data class that acts as a wrapper around the three variables. (You could also use the predefined Kotlin class Triple)

data class User(val name: String, val age: Int, val score: Int)

... creates a simple data class that fits your needs. Each instance of the data class represents the values of all three array lists at one index (one row on the table above).

Convert the lists

To convert your three lists to an instance of the User class, you can use the Kotlin extension function mapIndexed which transforms a list into a list of another type while providing us with the elements and its index, which we need to access the proper values in the other lists.

val users = name.mapIndexed { index, value -> User(value, age[index], score[index]) }

Now we have a list of users that looks like this:

[User(name=a, age=2, score=3), User(name=b, age=3, score=4), User(name=c, age=5, score=4), User(name=a, age=6, score=1)]

Sort the list

And we can now simply append the sortedByDescending function to order sort it in descending order:

val usersSorted = name.mapIndexed { index, value -> User(value, age[index], score[index]) }.sortedByDescending { it.score }

It is always best practice to use classes if you want to store more than two values of an object, especially in this case.

Go back to the original lists

If you (somehow) still need the original lists, you can get them back by using the map function. But please note that this form of data isn't very handy.

val name = users.map { it.name }
val age = users.map { it.age }
val score = users.map { it.score }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.