[英]How to sort a custom NSArray?
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? 我只想按名称排序。有什么想法吗?
您无法编辑NSArray
因为它是不可变的,因此您需要执行以下操作:
_items = [_items sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"itemName" ascending:YES selector:@selector(caseInsensitiveCompare:)]]];
The call to sortedArrayUsingDescriptors:
returns an NSArray instance, which is the sorted representation of the instance you call the method on. 调用
sortedArrayUsingDescriptors:
返回一个NSArray实例,该实例是您在其上调用该方法的实例的排序表示形式。 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. 如果要继续更改_items NSArray实例,则create是作为NSMutableArray开头的。
// 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:)]]];
TAke _items as NSMutableArray... 将_items作为NSMutableArray ...
NSArray *sortedArray;
sortedArray=[_items sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"itemName" ascending:YES selector:@selector(caseInsensitiveCompare:)]]];
[_items removeAllObjects];
[_items addObjectsFromArray:sortedArray];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.