0

So my idea, is to once is retrieved from the API, that they are shown alphabatecally by the value 'name' of the Charact object which are all them stored in to pokemons array.

I would like to know how i would be able to sort my list of Pokemons so they appear in alphabetical order

Here is my Object code:

class Charact: Codable  {
    
    var id: Int?
    var isDefault: Bool?
    var isMega : Bool?
    var isBattleOnly : Bool?
    var name: String?
    var sprites: Sprite?
    
    private enum CodingKeys: String, CodingKey {
        case isDefault = "is_default"
        case isMega = "is_mega"
        case isBattleOnly = "is_battle_only"
        case id
        case name
        case sprites
    }
    
}

Here is the code : Code in paste-bin

  • This is not SwiftUI -- this is UIKit – jnpdx May 26 '22 at 18:21
  • 2
    You need just to sort `pokemons`, like `pokemons = pokemons.sorted { $0.name < $1.name }` BUT, you have an issue, is that `images` index needs to be synched. You shouldn't use different array, but one. – Larme May 26 '22 at 18:47
  • Please show us the code of `Charact`. However, first just try to use Swift's standard `sort` methods on your array where you assign its value. What's the problem there? – lazarevzubov May 26 '22 at 18:47
  • 1
    It'd be better idea to define a new class or struct which contains both the pokemon and it's image (let's call it PokemonWithImage, then once you have the array of [PokemonWithImage] you can sort it in a similar way as @Larme mentioned. In this way you won't need to worry about the sequence of the images. And then you call tableView.reloadData() – Asteroid May 26 '22 at 19:24
  • Something that's not related to the question, is your downloadPokemonsInfo() function. API calls are asynchronous so there are better and more reliable ways to do that. see this: https://stackoverflow.com/questions/35906568/wait-until-swift-for-loop-with-asynchronous-network-requests-finishes-executing – Asteroid May 26 '22 at 19:28

1 Answers1

0

Assuming that you want to stick to your code, try this out:

func downloadPokemonsInfo(){
        for i  in 1...MAX_POKEMONS{
            connection.getCharact(withId: i) { charact in
                if let charact = charact, let id = charact.id{
                    self.pokemons[id-1] = charact
                    if let imageURL = charact.sprites?.frontDefault{
                        self.connection.getSprite(withURLString: imageURL) { image in
                            self.imagesDownload = self.imagesDownload + 1
                            if let image = image {
                                self.images[id - 1] = image
                            }
                            if self.imagesDownload == self.MAX_POKEMONS{
                                let zipped = zip(self.pokemons, self.images)
                                let zippedSorted = zipped.sorted { one, two in
                                    guard let c1 = one.0, let c2 = two.0, let n1 = c1.name, let n2 = c2.name else { return false }
                                    return n1 < n2
                                }
                                self.pokemons = zippedSorted.map { $0.0 }
                                self.images = zippedSorted.map { $0.1 }
                                DispatchQueue.main.async {
                                    self.tableView.reloadData()
                                    self.tableView.alpha = 1
                                }
                            }
                        }
                    }
                }
            }
        }
 
    }
Asteroid
  • 1,049
  • 2
  • 8
  • 16