dev-resources.site
for different kinds of informations.
Android Sorting CheatSheet
Published at
5/13/2020
Categories
sorting
android
java8
kotlin
Author
Aniket Kadam
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()
}
Articles
12 articles in total
Write-Only Social Media
read article
How to Raise Hell with a Vaccine Booking System
read article
Kotlin Flow - Implementing an Android Timer
read article
Snackbars Without Scaffolds (and why you should avoid them)
read article
How to Add a Snackbar to Jetpack Compose
read article
Notes on a Heap
read article
The Simplest Recycler EmptyView
read article
Dagger with a Hilt
read article
Intro to Testing: Why Test
read article
Android Sorting CheatSheet
currently reading
Effective Git, zero to max in small steps: First Steps
read article
Tips to Writing Tech Talks/Blog posts
read article
Featured ones: