Search code examples
objective-cprogramming-languagesios4

sort NSMutableArray using special pattern?


I want to sort an array something like this.

Suppose I have 4 users they log into their device like this

User 1 device (user id's 1,2,3,4)

User 2 device (user id's 1,2,3,4)

User 3 device (user id's 1,2,3,4)

User 4 device (user id's 1,2,3,4)

Now I want them to be sorted like this in each of their devices like clockwise.

User 1 device (user id's 1,2,3,4)

User 2 device (user id's 2,3,4,1)

User 3 device (user id's 3,4,1,2)

User 4 device (user id's 4,1,2,3)

Does someone knows any algorithm to achieve this?


Solution

  • You might be able to remove the first item of the array and then add it to the end and continue until the the user see's their username/id. This may work because as you remove the first item they are all shifted down to the first spot.

    while([[array objectAtIndex:0] id] != [user id])
    {
        id temp = [[array objectAtIndex:0] retain];
        [array removeObjectAtIndex:0]; //may cause a deallocation of "temp" if it weren't retained
        [array addObject:temp];
        [temp release];
    }