Search code examples
sortingkotlinviewmodelmutablelivedata

How to sort MutableLiveData<List<Book>>


I have MutableLiveData> in ViewModel. How can we sort it based on book name, id and so on.


Solution

  • You can use map function to transform your LiveData.

    val unsortedBooks: LiveData<Book> = //...
    val sortedBooks: LiveData<Book> = Transformations
        .map(unsortedBooks, Function { books ->
            //sort your `books` here and return the sorted list
        })