按升序对数组进行排序..?
抱歉问了愚蠢的问题..
我有一个包含类对象的数组,例如:
Class User {
NSString *firstName, *LastName;
}
@property (nonatomic, retail) NSString *firstName;
@property (nonatomic, retail) NSString *LastName;
我正在创建此类的对象并将其添加到 NSMutableArray 中。
现在我想按名字对这个数组进行排序..请问有什么想法吗?
我可以使用 NSSortDescriptor 来对该数组进行排序吗?或者我必须使用我的自定义逻辑。
我之前做过这件事,但现在我忘了这一点,而且现在没有源代码......
请帮助我...... 谢谢
编辑:
我得到了一些善意的回复,我尝试了但没有成功。我正在解析 JSON,因此 myArray 中会有未知数量的对象。所以我使用了 :
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"firstName" ascending:YES];
[myArray sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
但
NSMutableArray *tempArray = [myArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
没有任何效果..
Edit 2
以下是描述: 数组
"<User: 0xa3f13c0>",
"<User: 0xa3f1410>",
"<User: 0xa3f1450>",
"<User: 0xa3f1490>"
排序数组
"<User: 0xa3f13c0>",
"<User: 0xa3f1410>",
"<User: 0xa3f1490>",
"<User: 0xa3f1450>"
for 循环中的 : 数组
John
abc
test
qwertg
排序数组
John
abc
test
qwertg
Sorry for asking silly question..
I have an array with class objects like:
Class User {
NSString *firstName, *LastName;
}
@property (nonatomic, retail) NSString *firstName;
@property (nonatomic, retail) NSString *LastName;
I am creating object of this class and adding it in NSMutableArray.
Now I want to sort this array by the first name.. Any idea please.
Can I use NSSortDescriptor for sorting this array or I have to use my custom logic.
I had done this thing earlier but now I forgot this and don't have source code for that right now...
Please help me..
Thanks
EDIT:
I got few kind replies and I tried but not working. I am parsing JSON so there will be unknown number of objects in myArray. So I used :
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"firstName" ascending:YES];
[myArray sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
and
NSMutableArray *tempArray = [myArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
but nothing worked..
Edit 2
Here is the description:
Array
"<User: 0xa3f13c0>",
"<User: 0xa3f1410>",
"<User: 0xa3f1450>",
"<User: 0xa3f1490>"
Sorted Array
"<User: 0xa3f13c0>",
"<User: 0xa3f1410>",
"<User: 0xa3f1490>",
"<User: 0xa3f1450>"
In for Loop:
Array
John
abc
test
qwertg
Sorted Array
John
abc
test
qwertg
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的目标是 iOS4+,则可以使用
sortUsingComparator:
方法:Edit: 由于比较方法考虑字符大小写,您可能需要使用
caseInsensitiveCompare: 方法而不是它:
If you're targeting iOS4+ then you can use
sortUsingComparator:
method:Edit: As compare method takes into account character case you may need to use
caseInsensitiveCompare:
method instead of it:这可能是完成您正在寻找的操作的最干净的方法:
如果您不需要 ios4 之前的向后兼容性,则对数组进行排序的一种有趣方法是使用代码块,但这
将允许您在那里粘贴一些额外的逻辑,以防您将来会想要使用附加或其他排序条件。
This is probably the cleanest way to do what you're looking for:
A fun way to sort arrays is using code blocks, if you don't need pre ios4 backwards compatibility though
This will allow you to stick some additional logic there in case you'll want to use additional or other sorting conditions in the future.