How I can always sort a column in NSTableView ? when my view is loaded the data isn't sorted and also when the data is updated. For now only when I click on the header column the data is sort.
Here is my code to sort the data when clicking on the header column :
In ViewDidLoad :
let descriptorName = NSSortDescriptor(key: "designation", ascending: true, selector: #selector(NSString.caseInsensitiveCompare(_:)))
tableview.tableColumns[0].sortDescriptorPrototype = descriptorName
The delegate func :
func tableView(_ tableView: NSTableView, sortDescriptorsDidChange oldDescriptors: [NSSortDescriptor]) {
let dataArrayMutable = NSMutableArray(array: self.data)
dataArrayMutable.sort(using: tableView.sortDescriptors)
self.data = (dataArrayMutable as NSArray) as! [Article]
tableview.reloadData()
}
Problem solved : I had to sort the data in the data Array before loading the tableView. Here is the code :
data = data.sorted(by: { $0.designation! < $1.designation! })