From the course: Android Development Tips

Unlock this course with a free trial

Join today to access over 22,600 courses taught by industry experts.

Sort data collections with Kotlin

Sort data collections with Kotlin - Android Tutorial

From the course: Android Development Tips

Sort data collections with Kotlin

- [Instructor] In last week's tip, I described how to get started with Kotlin data collections. This week, I'll show you how to use some tools that let you easily sort, filter, and otherwise calculate and manipulate the contents of the data collection. To demonstrate some of the simplest functionality, I've created a list of strings. And I've packaged it in a class named DataProvider. It has a single function inside the companion object. That makes the function behave like a static method in Java, and it returns a simple list of strings, the names of the U.S. states in a randomized order. I'll come back to the main activity class and I'll come back to the shopping cart later. For the moment, I just want to work with that simple list of strings. I'm going to create a variable that I'll call stateList and I'll get its value from DataProvider.getStateList. Now I want to sort that data in alphabetical order. The list interface has a function called sorted. Now it doesn't actually sort…

Contents