-2

I made a function that sorts an Array and reorders a TableView (I followed this post). I populate the array through an API.

When or where is best to call the sorting function? When I call it from ViewDidLoad or ViewDidAppear the Array is still empty, so nothing happens.

func sort(){

    self.myArray = myArray.sorted(by: {$0.rank < $1.rank })
    self.tableView.reloadData()

}
Maruta
  • 1,063
  • 11
  • 24

5 Answers5

0

Call your function in ViewDidLoad but without reload table view data

func sort(){
    self.myArray = myArray.sorted(by: {$0.rank < $1.rank })    
}
A.Munzer
  • 1,890
  • 1
  • 16
  • 27
0

This is a tableView problem, check your delegate, data source and IB connections, nothing is wrong with the sorting as long as the array is not = nil (make sure of that)

Mostafa Sultan
  • 2,268
  • 2
  • 20
  • 36
0

Check you crash log, nothing wrong with this code

Manish Mahajan
  • 2,062
  • 1
  • 13
  • 19
0

I have no idea what you're using this sort for, so this may not be useful, but you can add a nil check at the top and init a new empty array if it the array is nil.

func sort(){
    if self.myArray == nil {
        self.myArray = [YourArrayType]()
    }
    self.myArray = myArray.sorted(by: {$0.rank < $1.rank })
    self.tableView.reloadData()

}  

You can also declare the array optional (private var myArray: [Your ArrayType]?) or initialize it upon declaration (private var myArray = [YourArrayType]()). The first lets you call any method on the array with a ? and it won't execute if the array is nil, the other one makes sure the array is not nil but rather just empty.

kevin
  • 442
  • 3
  • 13
  • thank you! I tried the first one and still nothing happens with the tableview. Is not nil, but empty. It seems that it needs time to load. And I am doing this test with only 3 elements on the tableview, is not that big.. – Maruta May 08 '18 at 09:03
  • @Maruta I don't know how you load your tableview data, but a very common way is to load some data from an API and use it to populate the tableview. If something like that is your case you can call `tableView.reloadData()` after finishing your data fetch to signal the tableview to call all its delegate functions again. – kevin May 08 '18 at 09:09
  • yes I am using an API and I also reloadData after data fetch. But where would you call the func sort() ? Everywhere I call it (except from an IBAction button) the array is not filled yet – Maruta May 08 '18 at 14:15
0

Finally what worked was calling the sorting function just after the array is created and reloading the tableview.

override func viewDidLoad() {
super.viewDidLoad()
getData()
}

func getData() {     

 Alamofire.request(apiURL).validate().responseJSON() { response in
            switch response.result {
            case .success:
                if let value = response.result.value {
                    let json = JSON(value)
                    let users = json.arrayValue
                    for item in users{
                        let temp = User(json: item)
                        self.myArray.append(temp)
                    }

                    self.tableView.reloadData()

                    self.sort()     //<---here

                }
            case .failure(let error):
                print(error)
            }
        }

func sort(){

    self.myArray = myArray.sorted(by: {$0.rank < $1.rank })
    self.tableView.reloadData()

}
Maruta
  • 1,063
  • 11
  • 24