title: How to reorder cells based on sort criteria programatically SWIFT 4
body: <p>I'm trying to reorder cells based on sorting criteria that will be triggered from a segment control. The problem that i am having when sorting it just sorts the labels in the table but some elements like a button that the text will change based on user interaction stays at the same row which do not make sense it should be more with the cell itself. Please see differences when tapped on the distance segment control it sorts the data correctly the problem is the button START ROUTE was pressed when it was at row 2 and changed into IN TRANZIT and now that is sorted it should be at row 1 but that is not the case here. The data that is populating the table is coming from core data. Can someone point me in the right direction, please?</p>
  
  <p><a href="https://i.stack.imgur.com/zW9aK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zW9aK.png" alt="enter image description here"></a>
  <a href="https://i.stack.imgur.com/qoI5Q.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qoI5Q.png" alt="enter image description here"></a></p>
  
  <pre><code>                func sortData() {
                      if segment.selectedSegmentIndex == 1 {
                          dropOffLocations.sort(by: {$0.dateCreated! &lt; $1.dateCreated! })
                      } else if segment.selectedSegmentIndex == 2 {
                          dropOffLocations.sort(by: {$0.distance &lt; $1.distance })
                      } else if segment.selectedSegmentIndex == 3 {
                          dropOffLocations.sort(by: {$0.postcode! &lt; $1.postcode! })
                      }
                      tableView.reloadData()
                  }
  
                  @IBAction func segmentChange(_ sender: Any) {
                      sortData()
                  }
  
  
    @objc func tappedButton(sender : UIButton) {
          if let cell = sender.superview?.superview?.superview as? UITableViewCell {
              if let indexPath = tableView.indexPath(for: cell) {
                  let dropOffLocation = dropOffLocations[indexPath.row]
                  sender.setTitle("IN TRANZIT", for: .normal)
                  sender.titleLabel?.adjustsFontSizeToFitWidth = true
                  dropOffLocation.btnStatus = sender.titleLabel?.text
                  sender.backgroundColor = #colorLiteral(red: 0.05882352963, green: 0.180392161, blue: 0.2470588237, alpha: 0.75)
  
                  save(completion: { (true) in
                      dropOffLocation.setValue("IN TRANZIT", forKey: "btnStatus")
                      print(dropOffLocation)
                  })
  
                  print(dropOffLocation)
                  let destinationCoordinate = CLLocationCoordinate2D(latitude: dropOffLocation.latitude, longitude: dropOffLocation.longitude)
                  let destinationPlacemark = MKPlacemark(coordinate: destinationCoordinate)
                  let destinationMapItem = MKMapItem(placemark: destinationPlacemark)
  
                  destinationMapItem.name = dropOffLocation.street
                  destinationMapItem.openInMaps(launchOptions: [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving])
  
              }
          }
      }
  
  
  
                func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
                      guard let cell = tableView.dequeueReusableCell(withIdentifier: "deliveryAddressCell", for: indexPath) as? AddressCell else { return UITableViewCell() }
  
                      let dropOffLocation = dropOffLocations[indexPath.row]
  
                      cell.startBtn.addTarget(self, action: #selector(self.tappedButton(sender:)), for: .touchUpInside)
                      cell.startBtn.titleLabel?.adjustsFontSizeToFitWidth = true
                      cell.startBtn.titleLabel?.minimumScaleFactor = 0.5
                      cell.startBtn.titleLabel?.numberOfLines = 1
                      cell.startBtn.titleLabel?.transform = CGAffineTransform(rotationAngle: CGFloat.pi / 2)
                      cell.configureCell(dropOffLocation: dropOffLocation)
                      cell.numberLbl.text = String(indexPath.row + 1)
  
                      return cell
                  }
  
  
        func save(completion: (_ finished: Bool) -&gt; ()) {
              guard let managedContext = appDelegate?.persistentContainer.viewContext else  { return }
              let dropOffLocations = DropOffLocation(context: managedContext)
  
              do {
                  try managedContext.save()
                  print("Succesfully saved data!")
                  completion(true)
              } catch {
                  debugPrint("Could not save: \(error.localizedDescription)")
                  completion(false)
              }
          }
  </code></pre>