76736

How to sort list of objects by two properties and collator

Question:

I have to sort list of object by lastName and firstName (if case there is the same lastName for multiple objects). I must also apply Collator to those.

Suppose I can do that for one property:

val collator = Collator.getInstance(context.getResources().getConfiguration().locale) myList.sortedWith(compareBy(collator, { it.lastName.toLowerCase() }))

But is it possible to apply also to that another restriction to also sort by firstName?

Answer1:

You can add another sort criteria with thenBy:

val comparator = compareBy(collator) { p: Person -> p.lastName.toLowerCase() } .thenBy(collator) { p: Person -> p.firstName.toLowerCase() } val result = myList.sortedWith(comparator)

Answer2:

Simplest would just be to concatenate the two properties in the selector lambda:

myList.sortedWith( compareBy(collator) { "${it.lastName} ${it.firstName}".toLowerCase() } )

Recommend

  • 2D string array(with local Turkish characters) sorting in java
  • android.content.res.Resources.getConfiguration()' on a null object reference
  • java import error “the type android.widget.Filter.FilterResults is not visible”
  • PHP ksort seems unaffected by setlocale
  • Adding ProgressBar to DetailsOverviewRow on Android TV
  • How to finish activity when date picker dialog on cancel button is clicked
  • Difference in performance between calling .localeCompare on string objects and constructing a purpos
  • Node.js decipher not works for other ciphers' ciphertext
  • What is the best equivalent of a main() function for an Android app?
  • Error uploading photos using Facebook SDK
  • Converting colour in Windows Phone
  • Best Way to create configuration file(config.php) php
  • Send XML using XMLHttpRequest using GWT
  • Server control behaving oddly
  • Debugging panics in Symbian OS using Carbide.c++
  • How to fetch the file list from gcs?
  • Capture reload/endrequest event after server redirect to download file
  • Synchronized and locks in singleton factory
  • Implementing different layouts for RecyclerView but getting class cast erro on onBindViewHolder
  • How to run code sequentially with parse, in Android?
  • Why have a private constructor [duplicate]
  • Populatate the spinner from Firebase database
  • write text on image and show it to a imageview
  • android Navigation Bar hiding and persantage of usable screen overlap
  • Can I have a variable number of URI parameters or key-value pairs in Laravel 4?
  • Simple linked list-C
  • Find Previous month name using Calender or any classes that in java
  • Group list of tuples by item
  • as3-flash: any way to access all the instances placed in different frames from document class?
  • IE11 throwing “SCRIPT1014: invalid character” where all other browsers work
  • how to save the state in userdefaults of accessory checkmark-iphone
  • Does it make sense to call System.gc() and Thread.sleep() when working on Bitmaps?
  • Caching attributes in superclass
  • copying resource to sdcard gives a damaged file in android
  • Database structure design with variable amounts of fields
  • jQuery .attr() and value
  • How do I fake an specific browser client when using Java's Net library?
  • How to get a value (ex: baseURL) in every Karate feature?
  • apache spark aggregate function using min value
  • Checking variable from a different class in C#