我怎么能按字母顺序排列NSMutableArray ?

时间:2021-10-31 15:59:31

I want to sort an NSMutableArray alphabetically.

我想按字母顺序对NSMutableArray进行排序。

6 个解决方案

#1


89  

You can do this to sort NSMutableArray:

你可以这样排序NSMutableArray:

[yourArray sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

#2


46  

The other answers provided here mention using @selector(localizedCaseInsensitiveCompare:)
This works great for an array of NSString, however the OP commented that the array contains objects and that sorting should be done according to object.name property.
In this case you should do this:

这里提供的其他答案提到了使用@selector(localizedCaseInsensitiveCompare:)这对NSString数组非常有用,但是OP评论说数组包含对象,排序应该根据object.name属性完成。在这种情况下,你应该这样做:

NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
[yourArray sortUsingDescriptors:[NSArray arrayWithObject:sort]];

Your objects will be sorted according to the name property of those objects.

您的对象将根据这些对象的名称属性进行排序。

#3


1  

NSSortDescriptor *valueDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES]; // Describe the Key value using which you want to sort. 
NSArray * descriptors = [NSArray arrayWithObject:valueDescriptor]; // Add the value of the descriptor to array.
sortedArrayWithName = [yourDataArray sortedArrayUsingDescriptors:descriptors]; // Now Sort the Array using descriptor.

Here you will get the sorted array list.

这里你会得到排序后的数组列表。

#4


0  

Use the NSSortDescriptor class and rest you will get every thing here

使用NSSortDescriptor类和rest你将得到这里的所有东西。

#5


0  

NSSortDescriptor * sortDescriptor;
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Name_your_key_value" ascending:YES];
NSArray * sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; 
NSArray * sortedArray;
sortedArray = [Your_array sortedArrayUsingDescriptors:sortDescriptors];

#6


0  

Maybe this can help you:

也许这能帮助你:

[myNSMutableArray sortUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"firstName" ascending:YES],[NSSortDescriptor sortDescriptorWithKey:@"lastName" ascending:YES]]];

All is acording to NSSortDescriptor...

所有的都是对NSSortDescriptor的acording。

#1


89  

You can do this to sort NSMutableArray:

你可以这样排序NSMutableArray:

[yourArray sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

#2


46  

The other answers provided here mention using @selector(localizedCaseInsensitiveCompare:)
This works great for an array of NSString, however the OP commented that the array contains objects and that sorting should be done according to object.name property.
In this case you should do this:

这里提供的其他答案提到了使用@selector(localizedCaseInsensitiveCompare:)这对NSString数组非常有用,但是OP评论说数组包含对象,排序应该根据object.name属性完成。在这种情况下,你应该这样做:

NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
[yourArray sortUsingDescriptors:[NSArray arrayWithObject:sort]];

Your objects will be sorted according to the name property of those objects.

您的对象将根据这些对象的名称属性进行排序。

#3


1  

NSSortDescriptor *valueDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES]; // Describe the Key value using which you want to sort. 
NSArray * descriptors = [NSArray arrayWithObject:valueDescriptor]; // Add the value of the descriptor to array.
sortedArrayWithName = [yourDataArray sortedArrayUsingDescriptors:descriptors]; // Now Sort the Array using descriptor.

Here you will get the sorted array list.

这里你会得到排序后的数组列表。

#4


0  

Use the NSSortDescriptor class and rest you will get every thing here

使用NSSortDescriptor类和rest你将得到这里的所有东西。

#5


0  

NSSortDescriptor * sortDescriptor;
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Name_your_key_value" ascending:YES];
NSArray * sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; 
NSArray * sortedArray;
sortedArray = [Your_array sortedArrayUsingDescriptors:sortDescriptors];

#6


0  

Maybe this can help you:

也许这能帮助你:

[myNSMutableArray sortUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"firstName" ascending:YES],[NSSortDescriptor sortDescriptorWithKey:@"lastName" ascending:YES]]];

All is acording to NSSortDescriptor...

所有的都是对NSSortDescriptor的acording。