views:

37

answers:

2

I have a group of objects (UIImageViews) in an array. I want to arrange them by yvalues. The greater the y value, the smaller the number in the array. So the one at the top of the page will be the first in the array and the one at the bottom is the last in the array. They move around so every second or so the array will have to be re-arranged.

Does anyone have any idea how I could do this? Any help would be greatly appreciated.

Cheers, Lite

A: 

Sorting an array controller is normally done via an array of NSSortDescriptors.

Just sorting an array can be done using sortedArrayUsingSelector:.

Bryan McLemore
Indeed however i've been having a problem with NSSort descriptors. To use a UIImageView with an NSSortDescriptor I need to set the key of the imageview as something and to do this I need to use .layer after it. Like this: for (UIImageView *Blocky in objectsArray){[Blocky.layer setValue:[NSString stringWithFormat: @"%f", Blocky.center.y] forKey:@"value"];}But when I come to write the NSSortDescriptor I can't use a .layer on the array which im sorting. So I just get an error. Is there any way around this?
Ozzie
have you thought about making a property that also sets the layer, and then returns the layer value. This might allow you to use the layer indirectly.
Bryan McLemore
Ah I haven't. I didn't realise you could do that! How could I go about doing it if you don't mind me asking? I couldn't find much about .layer online and im still a bit of an iPhone beginner.
Ozzie
I'm not much familar with layer honestly. All you'd need to do though is add a category that adds two methods, one to set layer and one to read it. Then you should be able to use those two methods to access it's value.
Bryan McLemore
Ok thanks for the help Bryan! If anyone could help explain what to do then it would be a great help! It's the last bit of my app and it's really stumped me because of the lack of online .layer information.
Ozzie
A: 

You want to sort by the frame's origin on the UIImageView.I have solved this problem in the past by making a wrapper object around the CGPoint, and doing your comparisons with sortUsingSelector.

David Sowsy