关于如何按其名称对SKShapeNode
元素数组进行排序的任何想法吗?让我们假设每个SKShapeNode.name
属性是一个数字100,23,31,...全部都分组为shapeNodesCollection
。应该怎么做才能将其整理到另一个数组shapeNodesCollectionSorted
中?在下面可以找到一些抽象代码。
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
属性以升序排序的形状。关于ios - 排序SKShapeNode元素的数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44952573/