SKShapeNode元素的排序数组
问题描述:
有关如何按照名称对SKShapeNode
元素的数组进行排序的任何想法?假设每个SKShapeNode.name
属性都是一个数字100,23,31,...所有这些都是组成shapeNodesCollection
。应该做些什么来整理成另一个阵列 - shapeNodesCollectionSorted
?下面你可以找到一些抽象的代码。SKShapeNode元素的排序数组
class Example: GameScene {
...
var shapeNodesCollection = [SKShapeNode]()
var shapeNodesCollectionSorted = [SKShapeNode]()
...
shapeNodesCollectionSorted = ... //sorted shapeNodesCollection
}
非常感谢任何人的任何贡献。
答
如果name
属性是一个简单的数字类型(例如,Int
,Float
,等),然后,这应该是足够的:
var shapeNodesCollectionSorted = shapeNodesCollection.sorted { $0.name < $1.name }
的shapeNodesCollectionSorted
然后将包含形状排序在升序根据顺序name
属性。
+0
这就是它!谢谢。 – RafalK
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 –
@Matrin R,非常感谢 – RafalK