title: Find the top 10 items in a JSON Dictionary based on an Int
body: <p>I am looking to pull the top 10 instances of a JSON dictionary based on which has the highest Int's. </p>
  
  <p>So for the example I'm showing, I'm looking for the top 10 ranked films based on their popularity ranking. I have posted an example of the dictionary below.</p>
  
  <p>Part of the Dictionary:</p>
  
  <pre><code>{
  "cast": [
  {
    "id": 201,
    "character": "Praetor Shinzon",
    "original_title": "Star Trek: Nemesis",
    "overview": "En route to the honeymoon of William Riker to Deanna Troi on her home planet of Betazed, Captain Jean-Luc Picard and the crew of the U.S.S. Enterprise receives word from Starfleet that a coup has resulted in the installation of a new Romulan political leader, Shinzon, who claims to seek peace with the human-backed United Federation of Planets. Once in enemy territory, the captain and his crew make a startling discovery: Shinzon is human, a slave from the Romulan sister planet of Remus, and has a secret, shocking relationship to Picard himself.",
    "vote_count": 643,
    "video": false,
    "media_type": "movie",
    "release_date": "2002-12-13",
    "vote_average": 6.2,
    "title": "Star Trek: Nemesis",
    "popularity": 7.61,
    "original_language": "en",
    "genre_ids": [
      28,
      12,
      878,
      53
    ],
    "backdrop_path": "/1SLR0LqYPU3ahXyPK9RZISjI3B7.jpg",
    "adult": false,
    "poster_path": "/n4TpLWPi062AofIq4kwmaPNBSvA.jpg",
    "credit_id": "52fe4226c3a36847f8007d05"
  },
  {
    "id": 855,
    "character": "Spec. Lance Twombly",
    "original_title": "Black Hawk Down",
    "overview": "When U.S. Rangers and an elite Delta Force team attempt to kidnap two underlings of a Somali warlord, their Black Hawk helicopters are shot down, and the Americans suffer heavy casualties, facing intense fighting from the militia on the ground.",
    "vote_count": 2540,
    "video": false,
    "media_type": "movie",
    "release_date": "2001-12-28",
    "vote_average": 7.3,
    "title": "Black Hawk Down",
    "popularity": 11.504,
    "original_language": "en",
    "genre_ids": [
      28,
      36,
      10752
    ],
    "backdrop_path": "/7u2p0VxnhVMHzfSnxiwz5iD3EP7.jpg",
    "adult": false,
    "poster_path": "/yUzQ4r3q1Dy0bUAkMvUIwf0rPpR.jpg",
    "credit_id": "52fe4282c3a36847f80248ef"
  },
  </code></pre>
  
  <p>From this dictionary what would be the right code used to pull the Top 10 film's based on their popularity ranking?</p>
  
  <p>Here is some of the code:</p>
  
  <pre><code> struct Cast: Codable {
      let title: String
      let character: String
      let poster_path: String?
      let id: Int
      let popularity: Double?
  }
  
  var filmCredits = [Cast]()
  </code></pre>
  
  <p>First issue I'm having is when I use <code>return 10</code> to return 10 results:</p>
  
  <pre><code>func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
      return 10
  }
  </code></pre>
  
  <p>I receive the error <code>Thread 1: Fatal error: Index out of range</code> when calling the indexPath in my <code>cellForItemAt</code> func.</p>
  
  <p>Here is the JSON Decoder Func:</p>
  
  <pre><code>func loadFilms() {
  
      let apiKey = ""
      let url = URL(string: "https://api.themoviedb.org/3/person/\(id)/combined_credits?api_key=\(apiKey)&amp;language=en-US")
      let request = URLRequest(
          url: url! as URL,
          cachePolicy: URLRequest.CachePolicy.reloadIgnoringLocalCacheData,
          timeoutInterval: 10 )
  
      let session = URLSession (
          configuration: URLSessionConfiguration.default,
          delegate: nil,
          delegateQueue: OperationQueue.main
      )
  
      let task = session.dataTask(with: request, completionHandler: { (data, response, error) in
          if let data = data {
              do {
                  let films = try! JSONDecoder().decode(Credits.self, from: data)
                  self.filmCredits = films.cast!
                  self.topCollection.reloadData()
  
              }
  
          }
  
          self.topCollection.reloadData()
  
      })
  
  
      task.resume()
  
  }
  </code></pre>
  
  <p>What I'm most unsure of is how to pull only the Top 10 ranked films. Would I use something similar to <code>filter</code> or <code>map</code>?</p>