Last active
April 27, 2022 13:14
-
-
Save mbobiosio/295e8b22b28fd8eab7f5855a56b4d86b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
/* | |
cards = ['Jack', 8, 2, 6, 'King', 5, 3, 'Queen'] | |
<!-- Required Output = [2,3,5,6,8,'Jack','Queen','King'] | |
Q: Sort the array as per the rules of card game using a generic method. | |
* */ | |
val card1 = arrayListOf("Jack", 8, 2, 6, "King", 5, 3, "Queen") | |
val card2 = arrayListOf( | |
"Jack", | |
8, | |
2, | |
6, | |
"King", | |
5, | |
3, | |
"Queen", | |
"Jack", | |
"King", | |
"Queen", | |
"Queen", | |
"King", | |
"Jack" | |
) | |
val card3 = arrayListOf("Jack", 8, 2, 6, 5, 3) | |
println(sortCards(card2)) | |
} | |
private fun sortCards(cards: ArrayList<*>): List<*> { | |
val order = arrayListOf("Ace", 2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "King") | |
cards.sortWith { p, p2 -> | |
order.indexOf(p) - order.indexOf(p2) | |
} | |
return cards | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment