按升序对数组进行排序..?

发布于 2024-11-03 19:23:21 字数 1349 浏览 1 评论 0原文

抱歉问了愚蠢的问题..

我有一个包含类对象的数组,例如:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦在深巷 2024-11-10 19:23:21

如果您的目标是 iOS4+,则可以使用 sortUsingComparator: 方法:

[yourArray sortUsingComparator:^(id obj1, id obj2){

        User* u1 = (User*)obj1;
        User* u2 = (User*)obj2;
        return [u1.firstName compare:u2.firstName];
    }];

Edit: 由于比较方法考虑字符大小写,您可能需要使用 caseInsensitiveCompare: 方法而不是它:

[yourArray sortUsingComparator:^(id obj1, id obj2){

        User* u1 = (User*)obj1;
        User* u2 = (User*)obj2;
        return [u1.firstName caseInsensitiveCompare:u2.firstName];
    }];

If you're targeting iOS4+ then you can use sortUsingComparator: method:

[yourArray sortUsingComparator:^(id obj1, id obj2){

        User* u1 = (User*)obj1;
        User* u2 = (User*)obj2;
        return [u1.firstName compare:u2.firstName];
    }];

Edit: As compare method takes into account character case you may need to use caseInsensitiveCompare: method instead of it:

[yourArray sortUsingComparator:^(id obj1, id obj2){

        User* u1 = (User*)obj1;
        User* u2 = (User*)obj2;
        return [u1.firstName caseInsensitiveCompare:u2.firstName];
    }];
酷遇一生 2024-11-10 19:23:21

这可能是完成您正在寻找的操作的最干净的方法:

NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"firstName" ascending:YES];
[myArray sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];

如果您不需要 ios4 之前的向后兼容性,则对数组进行排序的一种有趣方法是使用代码块,但这

[myMutableArray sortUsingComparator:
^(id a, id b) 
{
    User *userA = (User *)a;
    User *userB = (User *)b;
    return [userA.firstName compare:userB.firstName]; 
}];

将允许您在那里粘贴一些额外的逻辑,以防您将来会想要使用附加或其他排序条件。

This is probably the cleanest way to do what you're looking for:

NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"firstName" ascending:YES];
[myArray sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];

A fun way to sort arrays is using code blocks, if you don't need pre ios4 backwards compatibility though

[myMutableArray sortUsingComparator:
^(id a, id b) 
{
    User *userA = (User *)a;
    User *userB = (User *)b;
    return [userA.firstName compare:userB.firstName]; 
}];

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文