Is it possible to sort a collection of UIView NSArray based on their tag?

I saw examples of something like

[someArray sortUsingSelector:@selector(compare:)];

I am looking for an example that stores a collection of UIViews, and I would like to sort them in ascending or descending order based on tags that I assume programmatically when creating a new UIViews.

If someone can provide sample code or instructions that will be fantastic

+3
source share
2 answers

NSArray ( "" NSArray). , . , NSSortDescriptor:

NSSortDescriptor *ascendingSort = [[NSSortDescriptor alloc] initWithKey:@"tag" ascending:YES];
NSSortDescriptor *descendingSort = [[NSSortDescriptor alloc] initWithKey:@"tag" ascending:NO];
NSArray *sortedArray = [someArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:ascendingSort]];
+13
- (NSComparisonResult)compareTags:(UIView *)view {
    if ([self tag] == [view tag]) return NSOrderedSame;
    if ([self tag] > [view tag]) return NSOrderedDescending;
    return NSOrderedAscending;
}

, , , UIView ( ). , . Docs .

+2

Source: https://habr.com/ru/post/1740532/


All Articles