Reputation: 745
I have a list of String
as below:-
val a = listOf("G", "F", "E", "D", "C", "B", "A")
I will get another list from the server. For example:-
val b = listOf("A", "G", "C")
List from the server may contain fewer elements or more elements but will not contain elements other than the first list.
So, after sorting output should be like
// G, C, A
Upvotes: 2
Views: 4054
Reputation: 1230
You can use map
and sorted
to achieve that easily on the condition that a
do not have repetition -
val a = listOf("G", "F", "E", "D", "C", "B", "A")
val b = listOf("A", "G", "C")
val there = b.map{ v -> a.indexOf(v)}.sorted().map{v -> a[v]}
println(there)
Output:: [G, C, A]
Alternate sorter way as pointed by @jsamol in comment -
val there = b.sortedBy { a.indexOf(it) }
Upvotes: 6
Reputation: 18547
If I understand the requirement, what we're really doing here is filtering the first list, according to whether its elements are in the second. So another approach would be to do that explicitly.
val a = listOf("G", "F", "E", "D", "C", "B", "A")
val b = listOf("A", "G", "C").toSet() // or just setOf(…)
val there = a.filter{ it in b }
There's a bit more processing in creating the set, but the rest is faster and scales better, as there's no sorting or scanning, and checking for presence in a set is very fast.
(In fact, that would work fine if b
were a list; but that wouldn't perform as well for big lists.)
Upvotes: 1
Reputation: 10507
You are not trying to sort, you are trying to filter
fun filterByServer(server: List<String>, local: List<String>)
= local.filter { value -> server.contains(value) }
filter
takes a predicate in this case if your local value is contained on the server list
Upvotes: 5
Reputation: 323
You can create a custom comparator based on the indices of letters in the a list. Then use the List.sortedWith function to sort the b list. e.g.
val a = listOf("G", "F", "E", "D", "C", "B", "A")
val b = listOf("A", "G", "C")
val indexedA: Map<String, Int> = a.mapIndexed { index, s -> s to index }.toMap()
val comparator = object: Comparator<String> {
override fun compare(s1: String, s2: String): Int {
val i1 = indexedA[s1]
val i2 = indexedA[s2]
return if (i1 == null || i2 == null) throw RuntimeException("unable to compare $s1 to $s2")
else i1.compareTo(i2)
}
}
val c = b.sortedWith(comparator)
System.out.println(c)
I've converted the list a to a map: Letter to Index as an optimization.
Upvotes: 1