DEV Community

Aniket Kadam
Aniket Kadam

Posted on

1 1

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

fun arrangeNames(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 with thenBy calls.
A stream still needs to be created and collected for the result to be calculated.

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

Sentry growth stunted Image

If you are wasting time trying to track down the cause of a crash, it’s time for a better solution. Get your crash rates to zero (or close to zero as possible) with less time and effort.

Try Sentry for more visibility into crashes, better workflow tools, and customizable alerts and reporting.

Switch Tools 🔁

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more