diff --git a/src/main/scala/TopSongs/TopSongs.scala b/src/main/scala/TopSongs/TopSongs.scala index 465bdcd..d2f13be 100644 --- a/src/main/scala/TopSongs/TopSongs.scala +++ b/src/main/scala/TopSongs/TopSongs.scala @@ -113,6 +113,12 @@ case class TopSongs(songs: List[Song] = List()) { .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 = @@ -159,3 +165,16 @@ case class TopSongs(songs: List[Song] = List()) { 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") + } + + + + +