0

I have two classes namely ViewController and BViewController. The BViewController class has properties name, address, and rating.

Below is the code for ViewController.m

How do I go about arranging objects in the array such that they are ordered by rating ascending? Additionally, how do I display them in a view?

- (void)viewDidLoad {

        [super viewDidLoad];

    BViewController *b=[[BViewController alloc]init];
        b.name=@"x";
        b.address=@"bangalore";
        b.rating=@6;

    BViewController *c=[[BViewController alloc]init];
        c.name=@"y";
        c.address=@"bangalore";
        c.rating=@5;

    BViewController *d=[[BViewController alloc]init];
        d.name=@"z";
        d.address=@"bangalore";
        d.rating=@7;

    BViewController *e=[[BViewController alloc]init];
        e.name=@"abc";
        e.address=@"bangalore";
        e.rating=@4;

    BViewController *f=[[BViewController alloc]init];
        f.name=@"xyz";
        f.address=@"bangalore";
        f.rating=@8;
     }

2 Answers 2

3

Apple has an API to do what you want in an elegant way:

NSMutableArray *yourArray = [NSMutableArray arrayWithObjects:b, c, d, e ,f, nil];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"rating" ascending:YES];
[yourArray sortUsingDescriptors:@[sortDescriptor]];

Your array has now been sorted nicely.

Sign up to request clarification or add additional context in comments.

Comments

1

Your array

NSMutableArray *arrMain = [NSMutableArray arrayWithObjects:b, c, d, e ,f, nil];

Now sort based on rating key

NSArray *arrResult;
arrResult = [arrMain sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
    NSNumber *first = [(BViewController*)a rating];
    NSNumber *second = [(BViewController*)b rating];

    NSComparisonResult result =  [first compare:second];
    return  result;
}];

This is tested.

4 Comments

can u tell me what am suppose to take in storyBoard like textField or label to display the results,am new so am not getting how it goes!
@taj For Input, take UITextfields. UIButton for Sort Array Hit & UILabel to display result values.
hey, can u tell me how can i see output on console? because if i put NSLog(@"%@",arrResult); am getting some output like "<BViewController: 0x60800002ede0>", "<BViewController: 0x60800002f400>", "<BViewController: 0x60800002fa40>", "<BViewController: 0x60800002f9e0>", "<BViewController: 0x60800002fde0>" am not getting it why it is coming this way
@taj Because when you do NSLog(@"%@", b), that's what you'll get. To have a "meaningful" log, override -(NSString *)description method of BViewController, with something like return [NSString stringWithFormat:@"<%@ %p> name: %@, address: %@, rating: %@", [self class], self, _name, _address, _rating];

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.