feat: add function to retrieve top N songs by weeks on chart

This commit is contained in:
Rémi Heredero 2025-04-07 12:43:07 +02:00
parent 00336f0b55
commit 0c34a2df52
Signed by: Klagarge
GPG Key ID: 735B36B074A65F0F

View File

@ -113,6 +113,12 @@ case class TopSongs(songs: List[Song] = List()) {
.toMap .toMap
} }
// This function returns the top N songs by weeks on chart
// It sorts the songs by the streak value in descending order and takes the top N songs
def topNSongsByWeeks(songs: List[Song], n: Int): List[Song] = {
songs.sortBy(song => -song.rank._1.streak.getOrElse(0)).take(n)
}
} }
@main def main(): Unit = @main def main(): Unit =
@ -159,3 +165,16 @@ case class TopSongs(songs: List[Song] = List()) {
case (artist, count) => println(s"$artist: $count") case (artist, count) => println(s"$artist: $count")
} }
println("----------");
// print the top N songs by weeks on chart
val topNSongs = topSongs.topNSongsByWeeks(topSongs.songs, 5)
println("Top N songs by weeks on chart:")
topNSongs.foreach { song =>
println(s"${song.title} by ${song.singer.map(_.person.name).mkString(", ")} - ${song.rank._1.streak.getOrElse(0)} weeks")
}