0

I have a class officeSupply

@interface OfficeSupply : NSObject

@property (nonatomic, strong) NSString *itemName;
@property (nonatomic) int total;
@property (nonatomic) int price;
@property (nonatomic) int quantity;
@property (nonatomic, strong) UITextField *quantityText;
@property (nonatomic, strong) UIImage *itemPic;

-(id)initWithName:(NSString *)itemName total:(int)total price:(int)price quantity:(int)quantity picture:(UIImage *)itemPic;

@end

And in my view I have

 OfficeSupply *item1 = [[OfficeSupply alloc] initWithName:@"Stapler" total:0 price:50 quantity:0 picture:[UIImage imageNamed:@"stapler.jpg"]];
OfficeSupply *item2 = [[OfficeSupply alloc] initWithName:@"Paper" total:0 price:150 quantity:0 picture:[UIImage imageNamed:@"101291.jpg"]];
OfficeSupply *item3 = [[OfficeSupply alloc] initWithName:@"Pen" total:0 price:45 quantity:0 picture:[UIImage imageNamed:@"pen.jpg"]];
_items =[NSArray arrayWithObjects:item1, item2, item3, nil];

[_items sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"itemName" ascending:YES selector:@selector(caseInsensitiveCompare:)]]];

I've looked up the problem already but haven't been able to solve my problem.

The last line is supposed to sort it but I don't know why it won't.

I just want to sort them by name.. Any ideas?

1
  • 6
    It sorts the array just fine. But you never assign that sorted array to anything -- it just falls on the floor. Commented May 28, 2013 at 22:06

3 Answers 3

3

You can't edit an NSArray because it's immutable so you need to do:

_items = [_items sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"itemName" ascending:YES selector:@selector(caseInsensitiveCompare:)]]];
Sign up to request clarification or add additional context in comments.

Comments

3

The call to sortedArrayUsingDescriptors: returns an NSArray instance, which is the sorted representation of the instance you call the method on. So:

NSArray *sortedItems = [_items sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"itemName" ascending:YES selector:@selector(caseInsensitiveCompare:)]]];

You could also simply further by using literal syntax for your array of sort descriptors.

NSArray *sortedItems = [_items sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"itemName" ascending:YES selector:@selector(caseInsensitiveCompare:)]]];

If you want to keep mutating the _items NSArray instance then create is as an NSMutableArray to start with.

// Make sure _items instance variable is declared as an NSMutableArray    
_items =[NSMutableArray arrayWithObjects:item1, item2, item3, nil];

[_items sortUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"itemName" ascending:YES selector:@selector(caseInsensitiveCompare:)]]];

Comments

0

TAke _items as NSMutableArray...

NSArray *sortedArray;
sortedArray=[_items sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"itemName" ascending:YES selector:@selector(caseInsensitiveCompare:)]]];

[_items removeAllObjects];
[_items addObjectsFromArray:sortedArray];

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.