forked from Klagarge/MSE-AdvProg-MicroProject
		
	feat: add function to retrieve top N songs by weeks on chart
This commit is contained in:
		| @@ -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") | ||||
|   } | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user