sort NSArray of UIImagerview?
i want to sort NSArray of UIImageView according their frame?
i try for NSSortDescriptor but when i writ开发者_开发问答e key=@"frame" unrecognized selector error occur. please tell me how to sort my array.
thanks in advance.
You can always write your own comparator function and define custom sorting behavior by using NSArray sortedArrayUsingSelector method
try this code in your project
NSArray * myArray = @[
[[UIImageView alloc] initWithFrame:CGRectMake(0, 50, 200, 100)],
[[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 200, 100)],
[[UIImageView alloc] initWithFrame:CGRectMake(50, 40, 50, 100)],
[[UIImageView alloc] initWithFrame:CGRectMake(30, 30, 200, 100)],
[[UIImageView alloc] initWithFrame:CGRectMake(10, 50, 200, 67)]
];
myArray = [myArray sortedArrayUsingComparator:^NSComparisonResult(UIImageView * view1, UIImageView * view2) {
if (! CGRectIntersectsRect(view1.frame, view2.frame)) {
return NSOrderedAscending;
} else {
return NSOrderedDescending;
}
}];
精彩评论