I have an array of dictionaries.
Each dictionary is like this
object, @"myObject",
[NSValue valueWithCGRect:myRect], @"rect",
nil
How do I sort that array by myRect.origin.x ?
I know that objective-c has smart ways to sort arrays of dictionaries. My problem here is that I want to sort by the x coordinate of myRect and myRect is wrapped in a NSValue… any way to do that with a magic command? I know you guys are magicians! 😀
thanks.
The cleanest way is probably to use a method like
sortUsingComparator:orsortedArrayUsingComparator(depending on the mutability of the array) and define a custom comparator (a block taking two parameters and returning anNSComparisonResult) that unpacks your rects and compare their x-coordinates.edit: Well, since everyone else is providing code, here’s mine: