Ios 按其中一个数组的顺序对两个数组进行排序[Swift 3.0-Xcode 8]

Ios 按其中一个数组的顺序对两个数组进行排序[Swift 3.0-Xcode 8],ios,arrays,uitableview,sorting,structure,Ios,Arrays,Uitableview,Sorting,Structure,我有两个阵列: var ArrayToShowInTable = [String]() 我想按价格(从最小到最大)订购这两个阵列, 因此,我得到了如下结果: let list = ["Carrot", "Apple", "Toothbrush", "Pear", "Oranges"] let price = [3.50, 2.50, 1.50, 3.25, 4.25] 所以我可以加入他们 list = ["Toothbrush", "Apple", "Pear", "Carrot", "Or

我有两个阵列:

var ArrayToShowInTable = [String]()
我想按价格(从最小到最大)订购这两个阵列, 因此,我得到了如下结果:

let list = ["Carrot", "Apple", "Toothbrush", "Pear", "Oranges"]
let price = [3.50, 2.50, 1.50, 3.25, 4.25]
所以我可以加入他们

list = ["Toothbrush", "Apple", "Pear", "Carrot", "Oranges"]
price = [1.50, 2.50, 3.25, 3.50, 4.25]
然后在TableView中显示它

for i in 0...list.count-1{
let join = list[i] += "\(price[i])"
ArrayToShowInTable.append(join)
}
有没有办法做到这一点? 可能使用结构并打印到表


请帮助???

您可以使用字典数组

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


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "TableCell")
        cell.textLabel?.text = ArrayToShowInTable[indexPath.row] as? String
    return (cell)
let list=[“胡萝卜”:3.50,“苹果”:2.50,“牙刷”:1.50,“梨”:3.25,“橙子”:4.25]
让sortedList=fruitsDict.sorted{$0.value<$1.value}

为什么不创建对象:

let list = ["Carrot" : 3.50, "Apple" : 2.50, "Toothbrush" : 1.50, "Pear" : 3.25 , "Oranges" : 4.25]
let sortedList = fruitsDict.sorted{ $0.value < $1.value }
然后可以声明数组:

class Product{
    var price : Double = 0.0
    var name: String =""
    init(price:Double, name:String) {
         self.price = price
         self.name = name
    }
}
您只能使用
arrayToShowInTable

var arrayToShowInTable = [Product]()
然后,您可以按如下方式对其进行排序:

arrayToShowInTable = [Product(price:3.5, name:"Carrot"), Product(price:2.5 ,name: "Apple"), Product(price: 1.5,name: "Toothbrush"), Product(price: 3.25, name: "Pear"), Product(price: 4.25,name: "Oranges")]
arrayToShowInTable=arrayToShowInTable.sorted({$0.price<$1.price})

创建
字典
以匹配
产品
价格
。数组中的处理很难处理

arrayToShowInTable = arrayToShowInTable.sorted({$0.price < $1.price})
let product=[“胡萝卜”:3.50,“苹果”:2.50,“牙刷”:1.50,“梨”:3.25,“橙子”:4.25]
var sortedItem=product.sorted{(第一:(键:字符串,值:双精度),第二:(键:字符串,值:双精度))->Bool-in
返回first.value
您可以使用dictionary,这样价格和名称将同步,并且您将不得不担心在排序后同步它们的位置

使用sorted函数对字典进行排序,然后使用in table view函数

let product = ["Carrot" : 3.50, "Apple" : 2.50, "Toothbrush" : 1.50, "Pear" : 3.25, "Oranges" : 4.25]


var sortedItem = product.sorted { ( first : (key: String, value: Double), second:(key: String, value: Double)) -> Bool in
    return first.value < second.value
}

var productList = [String]()
var priceList =  [Double]()

for (key, value) in sortedItem  {
    productList.append(key)
    priceList.append(value)
}

print(productList)
print(priceList)
如果必须使用两个数组,则可以从这两个数组创建字典。
如果您不能使用字典,请告诉我。

字典无法排序,因为它们没有顺序。很抱歉,这将是字典数组。
let pricesList = ["Carrot": 3.50, "Apple": 2.50, "Toothbrush": 1.50, "Pear": 3.25, "Oranges": 4.25]

var result = pricesList.sorted(by: {(a,b) in
    a.value as Double! < b.value as Double!

}) //or sorted{ $0.value < $1.value }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return result.count
}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "TableCell")
        cell.textLabel?.text = "\(result[i].key) \(result[i].value)"
    return (cell)