如何对包含nsstring的NSMutableArray排序呢?

时间:2021-07-02 16:00:15

Is there an easy way to sort an NSMutableArray of NSMutableArrays containing NSStrings?

是否有一种简单的方法对包含nsstring的NSMutableArray排序?

I am sure that there must be an easy method for doing this but I can't seem to find it.

我确信一定有一种简单的方法可以做到这一点,但我似乎找不到。

To Clarify I want to sort the first array alphabetically by the NSString at index 3 of the sub array.

为了澄清这一点,我想按子数组索引3处的NSString的字母顺序对第一个数组进行排序。

4 个解决方案

#1


3  

Didn't see anyone actually answering this with other than "use this function, figure it out". So here is some actual code that you can use.

除了“使用这个函数,找出它”之外,没有人真正回答这个问题。这是一些你可以使用的实际代码。

Put this category at the end of your .h file (or anywhere and import the .h where ever you need to sort)

将此类别放在.h文件的末尾(或任何地方,并导入.h,在那里您需要排序)

@interface NSString (SortCompare)

-(NSInteger)stringCompare:(NSString *)str2;

@end

Put this in the .m file (or the .m of the .h you're importing)

将它放入.m文件(或您正在导入的.h的.m文件)

@implementation NSString (SortCompare)

-(NSInteger) stringCompare:(NSString *)str2
{
    return [(NSString *)self localizedCaseInsensitiveCompare:str2];
}

@end

Now use the sort by calling sortUsingSelector: on the array
NSMutableArray:

现在通过调用sortUsingSelector:对数组NSMutableArray:

[myArray sortUsingSelector:@selector(stringCompare:)];

NSArray:

NSArray:

[myArray sortedArrayUsingSelector:@selector(stringCompare:)];

#2


3  

If your NSMutableArray only contains objects of type NSString, simply do:

如果您的NSMutableArray只包含NSString类型的对象,那么只需:

[myMutableArray sortUsingSelector:@selector(compare:)];

#3


2  

As others have mentioned, NSArray doesn't have a compare: selector that allows it to sort itself, so you'll have to define it yourself. I would create a category on NSArray with a method called compare:(NSArray *)otherArray (or something that better describes what it does) and use that with sortUsingSelector:.

正如其他人提到的,NSArray没有compare: selector允许它自己排序,所以您必须自己定义它。我将在NSArray上使用名为compare:(NSArray *)otherArray(或更好地描述其功能的东西)的方法创建一个类别,并使用sortUsingSelector:。

Depending on your needs you could possibly stuff all your strings into a big array when you need to sort them, and use that instead. It could be a little less code to write.

根据您的需要,当您需要对字符串进行排序时,您可以将所有的字符串填充到一个大数组中,然后使用它。它可以是更少的代码。

#4


0  

You can sort by key with:

你可以按键排序:

NSSortDescriptor *aSortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"key" ascending:NO];
[yourArray sortUsingDescriptors:[NSArray arrayWithObject:aSortDescriptor]];

#1


3  

Didn't see anyone actually answering this with other than "use this function, figure it out". So here is some actual code that you can use.

除了“使用这个函数,找出它”之外,没有人真正回答这个问题。这是一些你可以使用的实际代码。

Put this category at the end of your .h file (or anywhere and import the .h where ever you need to sort)

将此类别放在.h文件的末尾(或任何地方,并导入.h,在那里您需要排序)

@interface NSString (SortCompare)

-(NSInteger)stringCompare:(NSString *)str2;

@end

Put this in the .m file (or the .m of the .h you're importing)

将它放入.m文件(或您正在导入的.h的.m文件)

@implementation NSString (SortCompare)

-(NSInteger) stringCompare:(NSString *)str2
{
    return [(NSString *)self localizedCaseInsensitiveCompare:str2];
}

@end

Now use the sort by calling sortUsingSelector: on the array
NSMutableArray:

现在通过调用sortUsingSelector:对数组NSMutableArray:

[myArray sortUsingSelector:@selector(stringCompare:)];

NSArray:

NSArray:

[myArray sortedArrayUsingSelector:@selector(stringCompare:)];

#2


3  

If your NSMutableArray only contains objects of type NSString, simply do:

如果您的NSMutableArray只包含NSString类型的对象,那么只需:

[myMutableArray sortUsingSelector:@selector(compare:)];

#3


2  

As others have mentioned, NSArray doesn't have a compare: selector that allows it to sort itself, so you'll have to define it yourself. I would create a category on NSArray with a method called compare:(NSArray *)otherArray (or something that better describes what it does) and use that with sortUsingSelector:.

正如其他人提到的,NSArray没有compare: selector允许它自己排序,所以您必须自己定义它。我将在NSArray上使用名为compare:(NSArray *)otherArray(或更好地描述其功能的东西)的方法创建一个类别,并使用sortUsingSelector:。

Depending on your needs you could possibly stuff all your strings into a big array when you need to sort them, and use that instead. It could be a little less code to write.

根据您的需要,当您需要对字符串进行排序时,您可以将所有的字符串填充到一个大数组中,然后使用它。它可以是更少的代码。

#4


0  

You can sort by key with:

你可以按键排序:

NSSortDescriptor *aSortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"key" ascending:NO];
[yourArray sortUsingDescriptors:[NSArray arrayWithObject:aSortDescriptor]];