I need to sort a list of Person
objects(List<Person>
, where each Person
object has few attributes like id
(unique), name
, age
… etc).
The sorting order is based on another list. That list contains a set of Person
id
's (A List<String>
which is already sorted).
What is the best way to order the List<Person>
in the same order as the list of id
's using Kotlin or Java.
Example:
List Person {
(“ID1”,”PERSON1”,22,..), (“ID-2”,”PERSON2”,20,..) ), (“ID-3”,”PERSON3”,19,..),…..
}
Ordered Id List :
List of ID {(“ID2”), (“ID1”),(”ID3”)….}
Sorted Person
list should be:
List PERSON {
(“ID-2”,”PERSON 2”,20,..) ), (“ID1”,”PERSON 2”,22,..), (“ID-3”,”PERSON 2”,19,..),…..
}
If the Person
list contains any id
's which are not mentioned in the id
list then those values should be at the end of the sorted list.
Edited: This is my current way in Java. I am hoping for a better way than this:
public static List<Person> getSortList(List <Person> unsortedList, List<String> orderList){
if(unsortedList!=null && !unsortedList.isEmpty() && orderList!=null && !orderList.isEmpty()){
List sortedList = new ArrayList<OpenHABWidget>();
for(String id : orderList){
Person found= getPersonIfFound(unsortedList, id); // search for the item on the list by ID
if(found!=null)sortedList.add(found); // if found add to sorted list
unsortedList.remove(found); // remove added item
}
sortedList.addAll(unsortedList); // append the reaming items on the unsorted list to new sorted list
return sortedList;
}
else{
return unsortedList;
}
}
public static Person getPersonIfFound(List <Person> list, String key){
for(Person person : list){
if(person.getId().equals(key)){
return person;
}
}
return null;
}
In order to reverse the original order of a list, you can use the reverse() method. The reverse() method is used to reverse the sequence of the list and not to arrange it in a sorted order like the sort() method. reverse() method reverses the sequence of the list permanently.
An efficient solution is to first create the mapping from the ID in the ids
(your desired IDs order) to the index in that list:
val orderById = ids.withIndex().associate { it.value to it.index }
And then sort your list of people
by the order of their id
in this mapping:
val sortedPeople = people.sortedBy { orderById[it.id] }
Note: if a person has an ID that is not present in the ids
, they will be placed first in the list. To place them last, you can use a nullsLast
comparator:
val sortedPeople = people.sortedWith(compareBy(nullsLast<String>) { orderById[it.id] })
I would do something like (in pseudocode, since i don't know what your code looks like)
listOfPersons = [{2,Bob},{3,Claire},{1,Alice}]
orderList = [1,3,2]
sortedList = []
for(id in orderList)
person = listOfPersons.lookup(id)
sortedList.add(person)
And the lookup would be easier if you would have a map (id-> person) rather than a listOfPersons.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With