I am using swapi.co as my source of json data and my response looks like the following: https://swapi.co/api/people/
My array of "characters" has the structure
// MARK: - CharactersResult
struct CharactersResult: Codable {
let name, height, mass, hairColor: String
let skinColor, eyeColor, birthYear: String
let gender: Gender
let homeworld: String
let films, species, vehicles, starships: [String]
let created, edited: String
let url: String
enum CodingKeys: String, CodingKey {
case name, height, mass
case hairColor = "hair_color"
case skinColor = "skin_color"
case eyeColor = "eye_color"
case birthYear = "birth_year"
case gender, homeworld, films, species, vehicles, starships, created, edited, url
}
}
I would like to get the smallest character and the largest out of my array. My function is:
func getCharacters(urlToGet: String){
do{
if let url = URL(string: urlToGet) {
URLSession.shared.dataTask(with: url) { data, response, error in
if let data = data {
do {
let jsonCharacters = try JSONDecoder().decode(Characters.self, from: data)
self.characters = self.characters + jsonCharacters.results
self.nextPageUrlForCharacters = jsonCharacters.next
self.updatePicker()
} catch let error {
print(error)
}
}
}.resume()
}
}
}
My main question is where to do the sorting and what is the most efficient way to get the smallest and the largest character.
reducefunction to get the smallest or largest. What do you mean by length? What makes a CharactersResult short or long?