2017-07-06 40 views
1

有關如何按照名稱對SKShapeNode元素的數組進行排序的任何想法?假設每個SKShapeNode.name屬性都是一個數字100,23,31,...所有這些都是組成shapeNodesCollection。應該做些什麼來整理成另一個陣列 - shapeNodesCollectionSorted?下面你可以找到一些抽象的代碼。SKShapeNode元素的排序數組

class Example: GameScene { 

    ... 

    var shapeNodesCollection = [SKShapeNode]() 
    var shapeNodesCollectionSorted = [SKShapeNode]() 

    ... 

    shapeNodesCollectionSorted = ... //sorted shapeNodesCollection 

} 

非常感謝任何人的任何貢獻。

+0

https://stackoverflow.com/questions/24130026/swift-how-to-sort-array-of-custom-objects-by-property-value,https://stackoverflow.com/questions/27337495/sort -a-skspritenode-array-according-to-specific-element –

+0

@Matrin R,非常感謝 – RafalK

回答

3

如果name屬性是一個簡單的數字類型(例如,IntFloat,等),然後,這應該是足夠的:

var shapeNodesCollectionSorted = shapeNodesCollection.sorted { $0.name < $1.name } 

shapeNodesCollectionSorted然後將包含形狀排序在升序根據順序name屬性。

+0

這就是它!謝謝。 – RafalK