1

I have an array of the objects from a Parse Class. Before I use it it needs to be sorted by its ID and then its sequence. I tried doing it his way...

var data = objects as [PFObject]!
data = data.sort({($0["Sequence"] as! Int) < ($1["Sequence"] as! Int)})
data = data.sort({($0["Identifier"] as! String) < ($1["Identifier"] as! String)})

... but its doesn't work. It first sorts it by sequence, then it sorts by ID but not keeping the sequence order. Is something wrong, or is there another way of sorting arrays with multiple conditions.

0

2 Answers 2

1

This should work

data = data.sort({
($0["Sequence"] as! Int) < ($1["Sequence"] as! Int) ||
  ( 
   ($0["Sequence"] as! Int) == ($1["Sequence"] as! Int) && 
   ($0["Identifier"] as! String) < ($1["Identifier"] as! String)
  )
})
Sign up to request clarification or add additional context in comments.

Comments

1

The answer is already found: https://stackoverflow.com/a/27040700/2799410 . Just substitute the compared values inside.

Comments