问题
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