Q:

Label's text inside of UICollectionViewCell to vary depending on if statement and array's value

I have a UICollectionView with multiple cells inside of it. Each cell has one label which contains the below data.
What I would like to do is for the labels text that aren't "A" or "B", I would like the value to be an Int value based on the order of the cell.
Example
Cells Label text at the moment:
A, A, 3, B, 5, A, B
What I would like:
A, A, 1, B, 2, A, B
Any help is greatly appreciated, many thanks!!
Code:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! DataCell

        let integerArray = [Int](1...10)
        let sortedCellData = cellData.sorted { $0.key < $1.key}

        if String(Array(sortedCellData)[indexPath.row].value) == "A" {
            cell.valueLabel.text = "A"
        } else if String(Array(sortedCellData)[indexPath.row].value) == "B" {
            cell.valueLabel.text = "B"
        } else {
            cell.valueLabel.text = String(integerArray[indexPath.row])
        }
        return cell
    }
}

A:

That's a strange sort.
This code should work:
let array: [Any] = ["A", "A", 3, "B", 5, "A", "B", "N"]
var index = 0
let sorted = array.compactMap { (element) -> Any? in
    if let asString = element as? String, (asString == "A" || asString == "B") {
        return asString
    } else {
        index = index + 1
        return String(index)
    }
}
print("array: \(array)")
print("sorted: \(sorted)")

Output:
$>array: ["A", "A", 3, "B", 5, "A", "B", "N"]
$>sorted: ["A", "A", "1", "B", "2", "A", "B", "3"]

Logic:
Iterate the elements: compactMap()
If it's a String, return it as such (the if let asString) if it's equals to "A" or "B"
If it's not, let's increment the "index" and return it as a String.
Recommendations: Do it before hand (before loading the UICollectionView), and once, not in collectionView(_:cellForItemAt:) because you are doing the same calculation each time, which is unnecessary, time consuming, and since it's done in main thread, you could see some latency.
Now if you keep sorted as an instance var, you just have to do cell.valueLabel.text = sorted[indexPath.row]

