Q: NSMutableArray counting occurrences of objects and then rearrange the array I have been seeking the best way in modifying a NSMutableArray which can hold multiple instances of the same object. I working also for iOS versions under 4.0 so using block is not the way to go.
Here's the situation:
I have an array like this:
ARRAY = [object1,object2,object3,object4,object5,object6,object7,object8];

Let's say that object2 object3 and object4 are actually the same objects. And object1 and object 7 as well. Then I would like to rearrange the array so that the most occurrences appear first and so on. So the array would have to look like this:
[object2,object3,object4,object1,object7,object5,object6,object8];

A: There are a few ways of doing that, one of which is by using an auxiliary NSCountedSet instance and a function that uses that NSCountedSet for comparison:
NSInteger countedSort(id obj1, id obj2, void *context) {
    NSCountedSet *countedSet = context;
    NSUInteger obj1Count = [countedSet countForObject:obj1];
    NSUInteger obj2Count = [countedSet countForObject:obj2];

    if (obj1Count > obj2Count) return NSOrderedAscending;
    else if (obj1Count < obj2Count) return NSOrderedDescending;
    return NSOrderedSame;
}

and
NSMutableArray *array = …;

NSCountedSet *countedSet = [[[NSCountedSet alloc] initWithArray:array]
    autorelease];

[array sortUsingFunction:countedSort context:countedSet];


Edit: extremeboredom has cleverly noticed that if two different objects have same repeat count then their corresponding elements are not necessarily contiguous in the resulting array. This solution should only be used in case it's not necessary for same objects to be contiguous.

Further edit: in case you need elements representing the same object to be contiguous, you could create a smaller array with the distinct elements only, sorted by their repeat count. Then, create another array with elements sorted by repeat count. Depending on your needs, you might not actually need the resulting array — maybe only distinctArray & the counted set suffice.
NSMutableArray *array = …;
NSCountedSet *countedSet = [[[NSCountedSet alloc] initWithArray:array]
    autorelease];

// Array with distinct elements only, sorted by their repeat count
NSArray *distinctArray = [[countedSet allObjects]
    sortedArrayUsingFunction:countedSort context:countedSet];

// Array with all the elements, where elements representing the same
// object are contiguous
NSMutableArray *sortedArray = [NSMutableArray arrayWithCapacity:[array count]];
for (id object in distinctArray) {
    for (NSUInteger i = 0; i < [countedSet countForObject:object]; i++) {
        [sortedArray addObject:object];
    }
}


A: What you need is NSBag, by Erica Sadun (GitHub).  Simple use case:
NSArray *objArray = @[ @"a", @"a", @"b", @"B", @"c", @"cc", @"c"];      
NSBag       *aBag = NSBag.new;

for ( id thing in objArray )     [aBag add:thing];   // fill the bag

for ( id unique in aBag.objects )                    // count'em out
         NSLog(   @"%@,     %i", 
                  unique,   [aBag occurrencesOf:unique] );

OUTPUT:
cc, 1
b, 1
B, 1
c, 2
a, 2


A: You can manage this by using isKindOfClass & isMemberOfClass instance methods. So simply loop through your Array and keep pushing items in new array based on your requirements
