1

This is the list:

[Videos(id=5, yt_id=yRPUkDjwr1A, title=test5, likes=0, kat=animals), Videos(id=4, yt_id=yRPUkDjwr1A, title=test4, likes=1, kat=pranks), Videos(id=3, yt_id=WkyUU9ZDUto, title=test3, likes=1, kat=pranks), Videos(id=2, yt_id=B_X9OQqtduE, title=test2, likes=0, kat=animals), Videos(id=1, yt_id=ywaKlGNiv80, title=test1, likes=0, kat=animals)]

How can I sort it by likes desc?

Thanks in advance!

1 Answer 1

7

You can use sortByDescending:

videos.sortByDescending { it.likes }

I think it is self explanatory.
If you want to assign the sorted result to another list use sortedByDescending:

val sortedList = videos.sortedByDescending { it.likes }

The result list's inferred type is List<Videos>, but if you want a MutableList:

val sortedList = videos.sortedByDescending { it.likes }.toMutableList()
Sign up to request clarification or add additional context in comments.

4 Comments

I tried: val finalvids = vids.sortByDescending { it.likes } but now finalvids is type "unit" and I can't use it as I would use vids, how to fix this?
Oh I see I don't need to put it in new val but still, can I do it somehow? Because I still need the old version too
sortedByDescending does not change the original list.
Change to val sortedList = videos.sortedByDescending { it.likes }.toMutableList()

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.