根据具有重复项的另一个数组的顺序对数组进行排序

时间:2019-01-19 20:59:16

标签: arrays swift sorting

我有两个数组-一个包含有序ID,另一个是具有ID属性的模型的数组。这是ID数组的示例:

ITCAw554sCHlN3A6q7Yl
n50cavoOJeRkjoRj11j3 
TE9ubEqV0tw0Hf4h8tk7 
TE9ubEqV0tw0Hf4h8tk7 
TE9ubEqV0tw0Hf4h8tk7 
ITCAw554sCHlN3A6q7Yl 
mkCs25zSf3J8QTvpS81b 
TE9ubEqV0tw0Hf4h8tk7 
Gc5lnHAKWm1OyeMkA9e7
n50cavoOJeRkjoRj11j3 
mkCs25zSf3J8QTvpS81b 
mkCs25zSf3J8QTvpS81b 
ITCAw554sCHlN3A6q7Yl 
X9WyrvEE253keNdlbVZc 
ITCAw554sCHlN3A6q7Yl

为了对第二个数组进行排序,我要做:

func sort(by ids: [String]) -> [Model] {
   return self.sorted{ ids.index(of: $0.id)! < ids.index(of: $1.id)! }
}

[Model].sort(by: idsArray)

这对于具有唯一项的数组很好用。但是,正如您在我的ID数组中看到的那样,我有重复的ID。因此,此调用的结果将导致此模型ID顺序:

ITCAw554sCHlN3A6q7Yl
ITCAw554sCHlN3A6q7Yl
ITCAw554sCHlN3A6q7Yl
ITCAw554sCHlN3A6q7Yl
n50cavoOJeRkjoRj11j3
n50cavoOJeRkjoRj11j3
TE9ubEqV0tw0Hf4h8tk7
TE9ubEqV0tw0Hf4h8tk7
TE9ubEqV0tw0Hf4h8tk7
TE9ubEqV0tw0Hf4h8tk7
mkCs25zSf3J8QTvpS81b
mkCs25zSf3J8QTvpS81b
mkCs25zSf3J8QTvpS81b
Gc5lnHAKWm1OyeMkA9e7
X9WyrvEE253keNdlbVZc

如您所愿,我无法匹配ID数组中的排序。那么,有没有一种优雅的方式来实现我所需要的?谢谢

1 个答案:

答案 0 :(得分:1)

我是为“操场”创建的):

struct Model {
    let id: String
    let placeId: String

    init(id: String, placeId: String) {
        self.id = id
        self.placeId = placeId
    }
}

extension Model {
    static func all() -> [Model] {
        let ids = ["ITCAw554sCHlN3A6q7Yl",
                   "n50cavoOJeRkjoRj11j3",
                   "TE9ubEqV0tw0Hf4h8tk7",
                   "TE9ubEqV0tw0Hf4h8tk7",
                   "TE9ubEqV0tw0Hf4h8tk7",
                   "ITCAw554sCHlN3A6q7Yl",
                   "mkCs25zSf3J8QTvpS81b",
                   "TE9ubEqV0tw0Hf4h8tk7",
                   "Gc5lnHAKWm1OyeMkA9e7",
                   "n50cavoOJeRkjoRj11j3",
                   "mkCs25zSf3J8QTvpS81b",
                   "mkCs25zSf3J8QTvpS81b",
                   "ITCAw554sCHlN3A6q7Yl",
                   "X9WyrvEE253keNdlbVZc",
                   "ITCAw554sCHlN3A6q7Yl"]
        var models: [Model] = []
        for (index, anId) in ids.enumerated() {
            let aModel = Model(id: anId, placeId: String(index))
            models.append(aModel)
        }
        return models
    }
}

然后,我这样做了:

let initialModels = Model.all()
let shuffled = initialModels.shuffled()
print("shuffled:\n\(shuffled)")

因此,shuffled的“顺序”错误。

您当前排序的问题是id不是唯一的。您需要对唯一的ID进行排序,这些ID可以通过对多个属性进行比较等来计算或计算得出。否则,您将无法确定找到的多个匹配项中的哪个是ID,根据您的说法,这些ID是id的组合和placeId属性。

let initialValues: [(String, String)] = initialModels.map({ ($0.id, $0.placeId) })

为了让您拥有:

func sort(by idsAndPlaceId: [(String, String)]) -> [Model]

然后,因为我不想使用self等,但这可能是适应性的:

let resorted = shuffled.sorted { (model1, model2) -> Bool in
    let model1Index = initialValues.firstIndex(where: { (id, placeId) -> Bool in
        return id == model1.id && placeId == model1.placeId
    })
    let model2Index = initialValues.firstIndex(where: { (id, placeId) -> Bool in
        return id == model2.id && placeId == model2.placeId
    })
    return model1Index ?? 0 < model2Index ?? 0
}
print("resorted:\n\(resorted)")

输出:

$>shuffled:
[Model(id: "ITCAw554sCHlN3A6q7Yl", placeId: "0"), 
Model(id: "n50cavoOJeRkjoRj11j3", placeId: "1"), 
Model(id: "mkCs25zSf3J8QTvpS81b", placeId: "10"), 
Model(id: "ITCAw554sCHlN3A6q7Yl", placeId: "5"),
Model(id: "X9WyrvEE253keNdlbVZc", placeId: "13"), 
Model(id: "n50cavoOJeRkjoRj11j3", placeId: "9"), 
Model(id: "ITCAw554sCHlN3A6q7Yl", placeId: "12"), 
Model(id: "TE9ubEqV0tw0Hf4h8tk7", placeId: "4"), 
Model(id: "mkCs25zSf3J8QTvpS81b", placeId: "11"), 
Model(id: "Gc5lnHAKWm1OyeMkA9e7", placeId: "8"), 
Model(id: "TE9ubEqV0tw0Hf4h8tk7", placeId: "3"), 
Model(id: "ITCAw554sCHlN3A6q7Yl", placeId: "14"), 
Model(id: "TE9ubEqV0tw0Hf4h8tk7", placeId: "2"), 
Model(id: "TE9ubEqV0tw0Hf4h8tk7", placeId: "7"), 
Model(id: "mkCs25zSf3J8QTvpS81b", placeId: "6")]
$>resorted:
[Model(id: "ITCAw554sCHlN3A6q7Yl", placeId: "0"), 
Model(id: "n50cavoOJeRkjoRj11j3", placeId: "1"), 
Model(id: "TE9ubEqV0tw0Hf4h8tk7", placeId: "2"), 
Model(id: "TE9ubEqV0tw0Hf4h8tk7", placeId: "3"), 
Model(id: "TE9ubEqV0tw0Hf4h8tk7", placeId: "4"), 
Model(id: "ITCAw554sCHlN3A6q7Yl", placeId: "5"), 
Model(id: "mkCs25zSf3J8QTvpS81b", placeId: "6"), 
Model(id: "TE9ubEqV0tw0Hf4h8tk7", placeId: "7"), 
Model(id: "Gc5lnHAKWm1OyeMkA9e7", placeId: "8"), 
Model(id: "n50cavoOJeRkjoRj11j3", placeId: "9"), 
Model(id: "mkCs25zSf3J8QTvpS81b", placeId: "10"), 
Model(id: "mkCs25zSf3J8QTvpS81b", placeId: "11"),
Model(id: "ITCAw554sCHlN3A6q7Yl", placeId: "12"), 
Model(id: "X9WyrvEE253keNdlbVZc", placeId: "13"), 
Model(id: "ITCAw554sCHlN3A6q7Yl", placeId: "14")]