sort a SKSpriteNode array according to a specific element

流过昼夜 提交于 2020-01-05 21:17:20

问题


I have a SKSpriteNode array declared like this :

class rgbNodes: SKSpriteNode
{
}

var colorNode = [rgbNodes]()

colorNode.append(rgbNodes(imageNamed: "Rectangle")) // every time we want to add a new element to this array

And I would like to sort every element of this array according to their position.x value, for example, if :

colorNode[0].position.x = 25
colorNode[1].position.x = 5
colorNode[2].position.x = 15

I want the array to be sorted like this :

colorNode[0].position.x = 5
colorNode[1].position.x = 15
colorNode[2].position.x = 25

But how can I manage to do it with the sort command ?


回答1:


To sort colorNode, you could do this:

colorNode.sort() {
    $0.position.x < $1.position.x
}


来源:https://stackoverflow.com/questions/27337495/sort-a-skspritenode-array-according-to-a-specific-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!