arrays - 如何在表格视图单元格中按字母顺序对 JSON 数据进行排序
问题描述
我从服务器以 JSON 格式获取用户数据。现在我可以获取用户详细信息,但它会自动按日期排序。以下是从服务器获取用户详细信息的代码:
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)
}
}
这是表格视图功能:
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
}
我想按用户名的字母顺序对它们进行排序。
重新加载 tableView(作为 JSON 对象)时的数据结构本质上是:
{"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"},
...
解决方案
您可以尝试使用如下排序函数对“self.marrDoctorList”进行排序:
self.marrDoctorList.sorted { $0. name.lowercased() < $1.name.lowercased() }
例如在您的代码中
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()
}
推荐阅读
- sql - 根据另一个参数按日期过滤
- flutter - 即使在授予存储权限后权限被拒绝
- python - Python pandas 没有像在 Oracle 上那样导入值
- shell - 查找快乐素数的 shell 代码中的问题
- python - 返回 Google SERP 上标题的链接地址
- c# - 如何知道用户何时按下某个 MediaPlayerElement 按钮(播放、暂停、下一曲目等)?
- gstreamer - 为什么 gstreamer tee 使用 udpsrc 失败?
- react-native - 创建水平列表以选择年龄
- java - Hibernate:如何通过连接字符串来创建 JPQL?
- php - PHP XML (DOM) 扩展:已安装但未在 centos 的 phpinfo 中列出