burg93
burg93

Reputation: 163

sorting SwiftyJSON results

I'm using SwiftyJSON to populate a tableview which is working fine but I'm struggling to find a way of sorting the data. I've put my code as I have a feeling there's a better way of storing and displaying the data as at the moment I'm putting it into separate arrays per json tag which makes sorting difficult. Very new to swift so help is appreciated. Can I sort the json result before using it, or perhaps there's a better way of storing it?

I want to sort the below based on time to print to table view as at the moment it just prints in order.

Example json:

[
  {
    "name": "John Doe",
    "time": 13683
  },
  {
    "name": "Dave Smith",
    "time": 20683
  },
  {
    "name": "Craig David",
    "time": 200
  }
]

Current approach (no sorting):

// Global variables
var tableName = [String]()
var tableTime = [String]()

func getJSON(){
    // Removed all the code here to get the JSON 

    let json = JSON(data: result!)

    dispatch_async(dispatch_get_main_queue(), {
        for item in json.arrayValue {
            if item["name"].stringValue != "" {
                self.tableName.append(item["name"].stringValue )
                self.tableTime.append(item["time"].stringValue)
            }
        }
        dispatch_async(dispatch_get_main_queue(),{
            self.tableView.reloadData()
        })
    })
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return tableName.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->
    UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as!TableViewCell
        // Configure the cell...

        cell.name.text = tableName[indexPath.row]
        cell.time.text = tableTime[indexPath.row]

        return cell

    }  
}

Upvotes: 4

Views: 1222

Answers (1)

vadian
vadian

Reputation: 285059

Use a custom struct as data model

struct Data {
    var name : String
    var time : String
}

Then you have only one array to sort

// Global variables
var dataSource = [Data]()

func getJSON(){
    // Removed all the code here to get the JSON 

    let json = JSON(data: result!)
    for item in json.arrayValue {
        let name = item["name"].stringValue
        if !name.isEmpty {
            self.dataSource.append(Data(name:name, time:item["time"].stringValue))
        }
    }
    self.dataSource.sortInPlace{ $0.name < $1.name}
    dispatch_async(dispatch_get_main_queue(),{
        self.tableView.reloadData()
    })
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSource.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->
    UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as!TableViewCell
        // Configure the cell...

        let data = dataSource[indexPath.row]
        cell.name.text = data.name
        cell.time.text = data.time

        return cell

    }  
}

Upvotes: 3

Related Questions