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

3
  • see this stackoverflow.com/questions/805547/… Commented May 16, 2012 at 5:02
  • still if you don't get then click bit.ly/LQlR4Z Commented May 16, 2012 at 5: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 Commented May 16, 2012 at 5:35

2 Answers 2

2
NSSortDescriptor *desc=[NSSortDescriptor  sortDescriptorWithKey:@"groupUser_state" ascending:YES];
NSArray *yourArray;
[yourArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:desc]];
Sign up to request clarification or add additional context in comments.

2 Comments

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 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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.