arraysswiftuitableviewpfquery

Sorting Tableview Rows By Multiple Arrays


I am currently trying to sort my tableview by the words on the labels in each row. Each row will have 3 things: a color, a animal type, and the name for the animal. I have a specific order in mind on how I want to organize the table, but depending on how the items are loaded, the order of the rows can be anything (the problem).

I want to sort these tables by color by this array: colorArray = ["red", "blue", "yellow", "green", "orange", "purple"]. This mean that all the red animals will be first while all the green ones will be in the middle etc. First problem is that I do not know how to sort an array by another string array. Second problem is that I need the other two arrays (animal and animal name) to change their order in accordance with the color array so the right animal and their names' will be with the correct color.

Example: if the color array are loaded in like blue, green, orange, red and the animal array is loaded in like dog, cow, cat, monkey, I will then need then need these two array to be sorted into red, blue, green orange and monkey, dog, cow, cat. This is so all the animals are with the right color. How do I fix these two problems? I have copied my current code at the bottom:

    func loadAnimals() {
            let animalQuery = PFQuery(className: "Animals")
            animalQuery.whereKey("userID", equalTo: PFUser.current()?.objectId! ?? String()) //getting which user
            animalQuery.limit = 10
            animalQuery.findObjectsInBackground { (objects, error) in
                if error == nil {
                    self.colorArray.removeAll(keepingCapacity: false)
                    self.animalNameArray.removeAll(keepingCapacity: false)                
                    self.animalTypeArray.removeAll(keepingCapacity: false)

                    for object in objects! {
                        self.colorArray.append(object.value(forKey: "colorType") as! String) // add data to arrays
                        self.animalNameArray.append(object.value(forKey: "animalName") as! String) // add data to arrays                    
                        self.animalTypeArray.append(object.value(forKey: "animalType") as! String) // add data to arrays
                    }
                    self.tableView.reloadData()

                } else {
                    print(error?.localizedDescription ?? String())
                }
            }
        }

    //places colors in rows
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! AnimalCell //connects to color cell

            //Adds the animal information in the cells
            cell.colorType.text = colorArray[indexPath.row] 
            cell.animalName.text = animalNameArray[indexPath.row] 
            cell.animalType.text = animalTypeArray[indexPath.row] 

            return cell
        }

Solution

  • Never use multiple arrays as data source. They are error-prone and annoying to maintain. Use a custom struct, this is a object oriented language.