Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Aniket Kadam
Aniket Kadam

Posted on

     

Android Sorting CheatSheet

For Kotlin

If there's just one value to compare, then the following can be used

names.sortedBy{it.length}

The two methods are used to compare the items in the list. If the first one returns equal, then the second will take over

funarrangeNames(names:List<String>):List<String>{names.sortedWith(compareBy({it.length},{it.length}))}

For Java

When using java, a comparator object has to be created, these can be chained withthenBy calls.
A stream still needs to be created and collected for the result to be calculated.

publicList<String>arrangeNames(List<String>names){Comparator<String>sizeComparator=Comparator.comparing(String::length).thenBy(String::length)returnnames.stream().sorted(sizeComparator).collect()}

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

An android dev with ~8 years of experience. I consult and may join fulltime for the right company.
  • Location
    India
  • Work
    Android Developer
  • Joined

More fromAniket Kadam

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp