在数组中排序Dictionarys

时间:2016-01-17 12:58:27

标签: xcode swift

我有一些dictionarys,我想根据它们的价格浮动将它们分类到数组中。

   // what I have
        product1 = ["name": "milk","price": 3.2]
        product2 = ["name": "bread","price": 2.9] 
        product3 = ["name": "meat","price": 4.1] 
        product4 = ["name": "sweets", "price": 1.0]
// what I want to convert it to
        priceArray = [1.0,2.9,3.2,4.1] 
        nameArray = ["sweets","bread","milk","meat"]

我想这样做,以便最便宜的产品在priceArray和nameArray中都是第一个,并且所有产品都按照这样排序

3 个答案:

答案 0 :(得分:2)

你可以这样做:

let product1 = ["name": "milk","price": 3.2]
let product2 = ["name": "bread","price": 2.9]
let product3 = ["name": "meat","price": 4.1]
let product4 = ["name": "sweets", "price": 1.0]

var tempDictArray = [[String: AnyObject]]()
tempDictArray.append(product1)
tempDictArray.append(product2)
tempDictArray.append(product3)
tempDictArray.append(product4)

func priceSort(dict1: [String: AnyObject], dict2: [String: AnyObject]) -> Bool {
    let price1 = dict1["price"] as? Float ?? 0
    let price2 = dict2["price"] as? Float ?? 0
    return price1 < price2
}


tempDictArray = tempDictArray.sort(priceSort)

var priceArray = [Float]()
var nameArray = [String]()

for item in tempDictArray {

    let price = item["price"] as! Float
    let name = item["name"] as! String

    priceArray.append(price)
    nameArray.append(name)
}
priceArray    //[1, 2.9, 3.2, 4.1]
nameArray     //["sweets", "bread", "milk", "meat"]

答案 1 :(得分:2)

1。模型

首先让我们使用一个结构来表示数据,它会比使用Dictionaries更清晰,更安全。

struct Product {
    let name: String
    let price: Double

    init?(dict:[String:Any]) {
        guard
            let name = dict["name"] as? String,
            let price = dict["price"] as? Double else {
                return nil
            }
        self.name = name
        self.price = price
    }
}

2。输入

现在给出了这个输入

let product1: [String: Any] = ["name": "milk","price": 3.2]
let product2: [String: Any] = ["name": "bread","price": 2.9]
let product3: [String: Any]  = ["name": "meat","price": 4.1]
let product4: [String: Any]  = ["name": "sweets", "price": 1.0]

我们只将4个字典放在一个数组

let productsDict = [product1, product2, product3, product4]

然后我们将字典数组转换为按Product(s)排序的数组price

3。已排序的产品数组

let products = productsDict.flatMap { Product(dict: $0) }.sort{ $0.price < $1.price }

4。提取价格和名称

最后我们将其转换为Double填充price属性

的数组
let prices = products.map { $0.price }

并填入String填充name属性的数组。

let names = products.map { $0.name }

答案 2 :(得分:1)

let product1 = ["name": "milk","price": 3.2]
let product2 = ["name": "bread","price": 2.9]
let product3 = ["name": "meat","price": 4.1]
let product4 = ["name": "sweets", "price": 1.0]

// Define a structure to hold your data.
// The protocol CustomStringConvertible allows you to format the
// data as a nice string for printing.

struct Product: CustomStringConvertible {
    var name: String
    var price: Double
    var description: String { return "name: \(name), price: \(price)" }

    init?(info: [String: NSObject]) {
        guard let name = info["name"] as? String,
              let price = info["price"] as? Double else { return nil }
        self.name = name
        self.price = price
    }
}

// Create an array of your products initializing them with the dictionary
// values.  Since the initializer for Product is failable (might return
// nil), here we use flatMap to get rid of any ones that failed to init.    
var products = [Product(info: product1), Product(info: product2), Product(info: product3), Product(info: product4)].flatMap {$0}

// Now it is easy to sort your product array on price.    
products.sortInPlace { $0.price < $1.price }

// Print out the products in order to show that they are sorted.    
products.forEach { print($0) }

// Use map to create the priceArray and nameArray from [products]
let priceArray = products.map { $0.price }  // [1.0, 2.9, 3.2, 4.1]
let nameArray = products.map { $0.name }    // ["sweets", "bread", "milk", "meat"]

输出:

name: sweets, price: 1.0
name: bread, price: 2.9
name: milk, price: 3.2
name: meat, price: 4.1