排序结构数组数组[数组] [结构]

时间:2017-02-24 21:59:19

标签: arrays swift for-loop struct enumeration

我有一个3D数组,它是通过枚举的for循环构建的,它执行本地SQL Lite表的后台查询。 99%的时间一切都井然有序。 1%的时间事情有点不合时宜。

由于执行后台任务的代码包含在具有完成处理程序的close中,因此我不确定序列为何变得有趣。下面是结构和数组的形成方式。

我在考虑对数组进行排序,但是无法获得语法并且不确定这是否是最好的方法?这感觉像是一种解决方法。

struct CollectionStruct {
    var name : String
    var description : String
    var title : String
    var image : PFFile
    var id: String
}

var packArray : [CollectionStruct] = []
var partArray : [CollectionStruct] = []
var multiPartArray : [[CollectionStruct]] = []
var tempPartArray : [[CollectionStruct]] = []

override func viewDidLoad() {
    super.viewDidLoad()

    // Built the self.packArray here in simalr fashion to below.
    // Always in correct order as the query to build it is sorted


    // Now build the partArray for each packArray

    // access with
    // self.multiPartArray[0][0].name

    for (index, item) in  self.packArray.enumerated() {

        print(index, item)

        // is it the index that is out of sequence?
        self.packId = self.packArray[index].id

        BuildArray.buildArrayFromQuery(queryForCollection: "Part", selectedPackID: self.packId, delegateSender: "DownloadPart", completeBlock: { (result) in

            if result.isEmpty == false {

                self.partArray = result
                // this is how i was appending the result.  But it will occasionaly append the results in the wrong order.
                // self.packArray[1], self.packArray[0].  Not sure if its because of the index from the enumeration or what??

                // self.multiPartArray.append(self.partArray)

                // this is where i was thinking of sorting the array ( or outside for loop ) this will do for not to test
                // set it to a temp so it can be sorted...
                self.tempPartArray.append(self.partArray)

                // now sort it.. dont know wtf to do here cant see anything online
                self.multiPartArray = self.tempPartArray.sorted(by: { ([CollectionStruct], [CollectionStruct]) -> Bool in
                    <#code#>
                })

            }
        })
    }
}

我无法弄清楚如何对此进行排序的语法。在另一个位置,我使用下面的代码对较小的struct数组进行排序:

self.packArray = result.sorted {
    $0.name < $1.name
}

----编辑----

.sorted函数的提示代码如下所示,其中有[CollectionStruct], [CollectionStruct]但是我的Struct(.name等)中的属性在排序中不可用。

self.multiPartArray = self.tempPartArray.sorted(by: { ([CollectionStruct], [CollectionStruct]) -> Bool in
     <#code#>
})

典型的$ 0&lt; $ 1 ect也不可用,因为我不能将二元运算符应用于2 Structs? 理想情况下,我可以按.name排序,因为这是一个顺序标识符。按顺序排序

self.multiPartArray[0][0].name < self.multiPartArray[0][1].name < .... so on

这是数据的示例输出,当它不按顺序时,您可以看到pk010在pk020之后,其他所有内容都按顺序排列。

[[Proj.CollectionStruct(name: "pk000-01", description: "This is an intro session. ", title: "Session 1", image: <PFFile: 0x7b91dff0>, id: "JKE5K4xOQA"), Proj.CollectionStruct(name: "pk000-02", description: "This is a lot of text.", title: "Session 2", image: <PFFile: 0x7b892a30>, id: "ft4o3EWyxX"), Proj.CollectionStruct(name: "pk000-03", description: "This is session 3", title: "Session 3", image: <PFFile: 0x7a6fa5f0>, id: "jebz0c8Bq1"), Proj.CollectionStruct(name: "pk000-04", description: "This is a test", title: "Session 4", image: <PFFile: 0x7b88e390>, id: "E2rGHJMcR7"), Proj.CollectionStruct(name: "pk000-05", description: "This is a set", title: "Session 5", image: <PFFile: 0x7b892320>, id: "dkRAUEbdUb"), Proj.CollectionStruct(name: "pk000-06", description: "Test", title: "Session 6", image: <PFFile: 0x7a6f68a0>, id: "ODTJd3gIml")],
 [Proj.CollectionStruct(name: "pk020-01", description: "This is an intro session", title: "Session 1", image: <PFFile: 0x7a6f7600>, id: "6uG2cJGxrZ"), Proj.CollectionStruct(name: "pk020-02", description: "Session 2", title: "Session 2", image: <PFFile: 0x7b878d60>, id: "6T6r8e8dS3"), Proj.CollectionStruct(name: "pk020-03", description: "Session 3", title: "Session 3", image: <PFFile: 0x7a7a0ff0>, id: "2hDSALAG0a"), Proj.CollectionStruct(name: "pk020-04", description: "Session 4", title: "Session 4", image: <PFFile: 0x7b910bd0>, id: "vd0eBYb3gr"), Proj.CollectionStruct(name: "pk020-05", description: "Session 5", title: "Session 5", image: <PFFile: 0x7b88f790>, id: "MS8ece8dr8"), Proj.CollectionStruct(name: "pk020-06", description: "Session 6", title: "Session 6", image: <PFFile: 0x7a7765c0>, id: "aDfmH8bFKU"), Proj.CollectionStruct(name: "pk020-07", description: "Session 7", title: "Session 7", image: <PFFile: 0x7b9eaf50>, id: "obfW0pZsBH"), Proj.CollectionStruct(name: "pk020-08", description: "Session 8", title: "Session 8", image: <PFFile: 0x7a783c20>, id: "cVmEefiirT")],
 [Proj.CollectionStruct(name: "pk010-01", description: "This is an intro session", title: "Session 1", image: <PFFile: 0x7a6f9b20>, id: "BPtmDXvzDF"), Proj.CollectionStruct(name: "pk010-02", description: "Session 2", title: "Session 2", image: <PFFile: 0x7a79ac00>, id: "B3MAHoFEYs"), Proj.CollectionStruct(name: "pk010-03", description: "Session 3", title: "Session 3", image: <PFFile: 0x7a627e40>, id: "Yg6TxTuyHi"), Proj.CollectionStruct(name: "pk010-04", description: "Session 4", title: "Session 4", image: <PFFile: 0x7a627f90>, id: "XTRUoKVH9P"), Proj.CollectionStruct(name: "pk010-05", description: "Session 5", title: "Session 5", image: <PFFile: 0x7a627b80>, id: "eO9xS05qsW"), Proj.CollectionStruct(name: "pk010-06", description: "Session 6", title: "Session 6", image: <PFFile: 0x7b879bd0>, id: "ePZdmFnnoQ"), Proj.CollectionStruct(name: "pk010-07", description: "Session 7", title: "Session 7", image: <PFFile: 0x7be58c50>, id: "BO6HGa7w1R"), Proj.CollectionStruct(name: "pk010-08", description: "Session 8", title: "Session 8", image: <PFFile: 0x7a69bd20>, id: "dwIxpmD2y8")],
 [Proj.CollectionStruct(name: "pk030-01", description: "Session 3", title: "Session 3", image: <PFFile: 0x7b87da60>, id: "lU1rm5cnbb")]]

-----编辑----

我仍然无法对此进行排序,但我至少一直在玩这个错误,它在另一个枚举的循环中并不太优雅,但它是我目前最好的大脑

// add a group for the for enumeration check
let insideGroup = DispatchGroup()

for (index, item) in self.tempPartArray.enumerated() {
    insideGroup.enter()
    if index >= 1 {
        // if the currentItem.name > the previousItem.name
        if item[0].name > self.tempPartArray[index - 1][0].name {
            print("The array is in sequence")
            insideGroup.leave()

        } else {
            print("the array is out of sequence")

            // exit gracefully
        }
    }
}
// the enumeration has finished you can set the variables
insideGroup.notify(queue: .main) {
    self.multiPartArray = self.tempPartArray
}

-----编辑----

即使将查询重写为同步任务仍然会不按时返回数据,因为一个查询可能需要比另一个更长的时间。

我认为我需要做的是使用axConcurrentOperationCount = 1

使查询成为一个操作的一部分

这应该迫使结果顺序。

我仍然想知道如何对这种类型的数组进行排序....

1 个答案:

答案 0 :(得分:1)

假设你的buildArrayFromQuery不是异步的,我以一种可以防止任何怪异的方式简化你的循环:

func findAllItems() {

    var allFoundItems: [[CollectionStruct]] = []
    for item in packArray {

        let packId = item.id

        BuildArray.buildArrayFromQuery(queryForCollection: "Part", selectedPackID: packId, delegateSender: "DownloadPart", completeBlock: { (result) in
            if result.isEmpty == false {
                allFoundItems.append(result)
            }
        })
    }
    multiPartArray = allFoundItems
}

如果buildArrayFromQuery是异步的,你应该重新评估你是如何做这一般的,然后做一些异步的东西,简单地获取所有数组并调用一个关闭它们的闭包。但如果你需要一种排序,这应该有效:

self.multiPartArray = tempPartArray.sorted  {
    // $0 is the left item, $1 is the right item.
    // However you're sorting your items, just put the logic in here.
    return $0 < $1
 }