如何在数组填充之后对其进行排序

时间:2017-12-06 14:41:09

标签: ios arrays swift swift4

   func getOkToDownload(response:AcloseCarsToDownload?, error : Error?) -> Void {

        if let response = response {
           self.response = response
            if response.status == "OK" {
                if let carsDownloaded = response.cars {
                    var number = numberCars / (categories?.count)!
                    number = number == 0 ? 1 : number

                 let sortedArray = carsDownloaded.sorted {
                            distance(from: currentLocation!, to: $0.location!) < distance(from: currentLocation!, to: $1.location!)
                    }


                /*    let firstPlace = sortedArray.prefix(1) */




                  cars.append(contentsOf: sortedArray.prefix(number))

            }
               self.tableView?.reloadData()
            } else {
                let alert = UIAlertController.init(title: "Error", message: response.status, preferredStyle: .alert)
                alert.addAction(UIAlertAction.init(title: "Cancel", style: .cancel, handler: nil))
                alert.addAction(UIAlertAction.init(title: "Retry", style: .default, handler: { (action) in
                    DispatchQueue.main.async {
                        self.loadPlaces(true)

                    }
                }))
                 self.present(viewController: alert)
                }
            isLoading = false
        }
        else {
            print("response is nil")
        }
    }

我正在使用此功能从服务器创建下载项目列表cars.append(contentsOf: sortedArray.prefix(number));我希望这个列表从最接近最公平的项目排序,所以我想通过添加

来订购它
let sortedArray = carsDownloaded.sorted {
                            distance(from: currentLocation!, to: $0.location!) < distance(from: currentLocation!, to: $1.location!)
                    }

但是这个解决方案的问题是它在每次调用时对列表进行排序,结果不是所需的,我希望只有一次完成并且数组被填充,列表才会被排序。我该怎么办?

这是我用于通话的功能

func loadCars(_ force:Bool) {

        if !force {
            if !canUpdate() {
                return
            }
        }

         if(force){
        print("load")
        iUpdating = true
         for category in categories! {

            CloseCars.getCloseCars(by: category?.name ?? "berl", coordinates: currentLocation!, radius: radius, token: self.response?.nextPageToken, completion: getOkToDownload)

       }
    }
 }

1 个答案:

答案 0 :(得分:0)

正如我在评论中所说,您可以计算回复数量并将其与类别数量进行比较,然后才执行排序功能。我没有测试过代码,但你可以试试这样的东西

    var counter: Int = 0
var sortedArray: [String] = [] {
    didSet {
        self.tableView.reloadData()
    }
}

func getOkToDownload(response:AcloseCarsToDownload?, error : Error?) -> Void {
    counter += 1
    guard let response = response
        else {
            print("response is nil")
            return
    }
    if response.status != "OK" {return}
    guard let downloadedCars = response.cars else { return }
    allCars.append(contentsOf: downloadedCars)
    if counter != categories.count {return}
    let sortedArray = allCars.sorted {
        distance(from: currentLocation!, to: $0.location!) < distance(from: currentLocation!, to: $1.location!)
    }
}