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
For further actions, you may consider blocking this person and/orreporting abuse