sort tmdb results with String.compareTo

pull/43/head
Jannik 2 years ago
parent 3fcd1a96b2
commit 5555269877
Signed by: Seil0
GPG Key ID: E8459F3723C52C24

@ -38,10 +38,48 @@ class TMDBApiController {
private suspend fun searchTVShow(title: String): TMDBResponse = withContext(Dispatchers.IO) {
val url = URL("$searchTVUrl$preparedParameters&query=${URLEncoder.encode(title, "UTF-8")}")
val response = JsonParser.parseString(url.readText()).asJsonObject
//println(response)
println(url)
println(response)
return@withContext if (response.get("total_results").asInt > 0) {
response.get("results").asJsonArray.first().asJsonObject.let {
val results = response.get("results").asJsonArray
var result = results.asJsonArray.first()
var bestMatch = Int.MAX_VALUE
results.forEach { it ->
val name = getStringNotNull(it.asJsonObject, "name")
val rating = name.compareTo(title)
println("name: $name, title: $title")
when {
rating < 0 -> {
println("rating < ($rating): $it")
if ((rating * -1) < bestMatch) {
bestMatch = (rating * -1)
result = it
println("best < ($bestMatch): $it")
}
}
rating == 0 -> {
println("rating == ($rating): $it")
bestMatch = 0
result = it
println("best == ($bestMatch): $it")
}
else -> {
println("rating > ($rating): $it")
if (rating < bestMatch) {
bestMatch = rating
result = it
println("best > ($bestMatch): $it")
}
}
}
}
result.asJsonObject.let {
val id = getStringNotNull(it, "id").toInt()
val overview = getStringNotNull(it, "overview")
val posterPath = getStringNotNullPrefix(it, "poster_path", imageUrl)

Loading…
Cancel
Save

Du besuchst diese Seite mit einem veralteten IPv4-Internetzugang. Möglicherweise treten in Zukunft Probleme mit der Erreichbarkeit und Performance auf. Bitte frage deinen Internetanbieter oder Netzwerkadministrator nach IPv6-Unterstützung.
You are visiting this site with an outdated IPv4 internet access. You may experience problems with accessibility and performance in the future. Please ask your ISP or network administrator for IPv6 support.
Weitere Infos | More Information
Klicke zum schließen | Click to close