1

My NSMutableArray contain the list of GroupUser objects :

@interface GroupUser : NSObject {

    NSString *groupUser_name;//user name
    NSString *groupUser_customMsg;
    NSString *groupUser_emailId;
    NSString *groupUser_groupName;
    NSString *groupUser_aliasName;
    int groupUser_imageId;
    int groupUser_state;//whether user is online( value 0) offline(value 1) or busy(value 2)
    int groupUser_type;             
}

now i want to sort the list : 1. On the bases of groupUser_state 2. On the bases of groupUser_name

eg:: 1. Sam Offline(value 1) 2. Ravi Online(value 0) 3. Amit Online(value 0) 4. Ganga Online(value 0) 5. Durga Offline(value 1)

Output after sorting should be:: 1. Amit Online 2. Ganga Online 3. Ravi Online 4. Durga Offline 5. Sam Offline

Help me by providing code for the same.. Thanx in advance

Maulik
  • 19,348
  • 14
  • 82
  • 137
Saraswati
  • 1,516
  • 1
  • 14
  • 21
  • see this http://stackoverflow.com/questions/805547/how-to-sort-an-nsmutablearray-with-custom-objects-in-it – HarshIT May 16 '12 at 05:02
  • still if you don't get then click http://bit.ly/LQlR4Z – HarshIT May 16 '12 at 05:07
  • i have tried sorting by name(using sortedArrayUsingComparator) and its done ... but i want to sort it by state first and then by the name – Saraswati May 16 '12 at 05:35

2 Answers2

2
NSSortDescriptor *desc=[NSSortDescriptor  sortDescriptorWithKey:@"groupUser_state" ascending:YES];
NSArray *yourArray;
[yourArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:desc]];
Allamaprabhu
  • 845
  • 1
  • 7
  • 17
  • Thanx every one who answered. With help of all links i got my solution NSArray *sortedArray; NSSortDescriptor* stateDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"groupUser_state" ascending:NO] autorelease]; NSSortDescriptor* nameDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"groupUser_name" ascending:YES] autorelease]; NSArray* sortDescriptors = [NSArray arrayWithObjects:stateDescriptor, nameDescriptor, nil]; sortedArray = [[m_appDelegate groupUsersList] sortedArrayUsingDescriptors:sortDescriptors]; Feel free to comment on this solution .. if in any sense i can improve it – Saraswati May 16 '12 at 06:01
  • @Saraswati When adding code like this, it is a good idea to edit your original question and add the solution to the end or add your own answer so that people can actually read it. – lnafziger May 16 '12 at 15:50
0

Alternatively, you can call NSMutableArray's -sortUsingSelector:, but your custom objects should provide a method for making the comparisons, something like this:

- (NSComparisonResult) compareSomeVariable:(GroupUser*) otherUser
{
    // Assumes ivar _someVariable is numeric; use NSString methods to compare
    // strings, etc.

    if(this->_someVariable < otherObject->_someVariable){
        return NSOrderedAscending;
    }
    else if(this->_someVariable > otherObject->_someVariable){
        return NSOrderedDescending;
    }
    else{
        return NSOrderedSame;
    }
}

and the sort them like this:

[array sortUsingSelector:@selector(compareSomeVariable:)];

The drawback is that you have to define this custom method in your class. You can have several different methods to sort your objects according to different criteria.

Nicolas Miari
  • 16,006
  • 8
  • 81
  • 189