简体   繁体   中英

Objective C - Sorting by property

Is there anyway I can sort a NSMutableArray that contains UIImageView by the UIImageView's frame.origin.y value?

Trying to do the sort before adding the view to make sure the stacking order is correct.

Thank you, Tee

You could provide a compare selector:

NSInteger intSort(id num1, id num2, void* context)
{
    int v1 = [num1 intValue];
    int v2 = [num2 intValue];
    if (v1 < v2)
        return NSOrderedAscending;
    else if (v1 > v2)
        return NSOrderedDescending;
    else
        return NSOrderedSame;
}

This method compares NSNumber objects.
You would need to change it to change your view controller coordinates.

To set this compare selector:

NSArray* sortedArray; 
sortedArray = [anArray sortedArrayUsingFunction:intSort context:NULL];

This is an example from the Apple documentation .


Here is a similar (but for alphabetical sorting) question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM