Uchiha Sasuke
Uchiha Sasuke

Reputation: 157

How to sort JSON data alphabetically in tableview cell

I get user datas from server in JSON format. Now I can get user details but it's sorted by date automatically. Here is the codes that get user details from server:

func getAllDoctor(){

    let param = ["page":"1"]
    print(param,"param123")
    Alamofire.request(helper.MainURL + "patient/getAllDoctor", method:.post,parameters: param).responseJSON { response in

        self.stopAnimating()

        if let result = response.result.value {
            print(1)
            let DictResponse = JSON(result as! NSDictionary)

            if DictResponse["success"].boolValue
            {
                self.marrDoctorList = DictResponse["data"].arrayValue                    
                self.tblDoctors.reloadData()
            }

        }

        }.responseString { (strREsopns) in
            print(strREsopns)
    }

}

Here is the tableview functions:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return  UITableView.automaticDimension
}

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! AllDoctorsTVC

    cell.selectionStyle = .none
    cell.viewBG.layer.cornerRadius = 5
    cell.viewBG.layer.borderWidth = 1
    cell.viewBG.layer.borderColor = UIColor.lightGray.cgColor

    cell.lblName.text = self.marrDoctorList[indexPath.row]["name"].stringValue
    cell.lbl1.text = self.marrDoctorList[indexPath.row]["categories_name"].stringValue

    cell.imgDoctor.sd_setImage(with: URL(string: marrDoctorList[indexPath.row]["profile_image"].stringValue), placeholderImage: UIImage(named: ""))
    cell.imgDoctor.layer.cornerRadius = cell.imgDoctor.frame.height / 2
    cell.imgDoctor.clipsToBounds = true

    cell.imgRead.isHidden = true

    return cell
}

I want to sort them by user names alphabetically.

The data structure at the point the tableView (as JSON object) is reloaded is essentially:

{"success":true,"data":[
{"doctor_id":"149","name":"Ferit Dadasli","description":"Ortodontist","expertise_categories":"2","categories_name":"Di\u015f Hekimi","profile_image":"http:\/\/www.....net\/app\/assets\/default\/user1.png"},
{"doctor_id":"141","name":"Ahmet Karaman","description":"Pedodontist","expertise_categories":"1","categories_name":"Doktor","profile_image":"http:\/\/www.....net\/app\/assets\/default\/user1.png"},
...

Upvotes: 0

Views: 259

Answers (1)

Erez Mizrahi
Erez Mizrahi

Reputation: 197

you can try sort "self.marrDoctorList" using sort function like this :

self.marrDoctorList.sorted { $0. name.lowercased() < $1.name.lowercased() } 

for example in your code

        if DictResponse["success"].boolValue
        {
            self.marrDoctorList = DictResponse["data"].arrayValue.sorted { $0. name.lowercased() < $1.name.lowercased() }    

//or
    self.marrDoctorList.sorted { $0. name.lowercased() < $1.name.lowercased() } 

            self.tblDoctors.reloadData()
        }

Upvotes: 2

Related Questions