基于TableView的一个数组对多个数组进行排序

时间:2017-09-25 12:53:14

标签: ios arrays swift3

我有表视图和三个数组,其值被添加到tableview单元格。我想基于nearDistance asc对nearbyName和nearbyAddress进行排序。目前数组如下所示:

nearbyName = ["Place 1", "Place2", "Place3"]
nearbyAddress = ["Address 1", "Address 2", "Address 3"]
nearbyDistance = [71.00, 50.02, 73.06]

我需要对它们进行排序并获得结果

nearbyName = ["Place 2", "Place1", "Place3"]
nearbyAddress = ["Address 2", "Address 1", "Address 3"]
nearbyDistance = [50.02, 71.00, 73.06]

将来我会从JSON获得这个数组,所以我需要自动对它们进行排序。完整代码吼叫

import UIKit
import Foundation

class NearbyController: UIViewController, UITableViewDataSource, UITableViewDelegate{


@IBOutlet weak var tableView: UITableView!

let nearbyName = ["Place 1", "Place2", "Place3"]
let nearbyAddress = ["Address 1", "Address 2", "Address 3"]
let nearbyDistance = [71.00, 50.02, 73.06]


override func viewDidLoad() {
    super.viewDidLoad()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cellNearby") as! NearbyCell
    cell.nearbyClubName.text = nearbyName[indexPath.row]
    cell.nearbyClubAddress.text = nearbyAddress[indexPath.row]
    cell.nearbyClubDistance.text = nearbyDistance[indexPath.row]
    return cell
}
}

0 个答案:

没有答案