`
wsqwsq000
  • 浏览: 689927 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

NSArray的排序功能sortedArrayUsingSelector

 
阅读更多

 

- (NSArray *)sortedArrayUsingSelector:(SEL)comparator

Parameters
comparator

A selector that identifies the method to use to compare two elements at a time. The method should returnNSOrderedAscending if the receiver is smaller than the argument, NSOrderedDescending if the receiver is larger than the argument, and NSOrderedSame if they are equal

 

NSArray *sortedArray =
    [anArray sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];

 

 

 


@property (nonatomic,readwriteretain) NSMutableArray *parameters;

 

[self.parameterssortUsingSelector:@selector(compare:)];


 

 

#pragma mark -

 

- (NSComparisonResult)compare:(id)inObject {

NSComparisonResult result = [self.namecompare:[(MPURLRequestParameter *)inObjectname]];

 

if (result ==NSOrderedSame) {

result = [self.valuecompare:[(MPURLRequestParameter *)inObjectvalue]];

}

 

return result;

}

 

//////////////////////////////////////////////////////////

 

sortedArrayUsingFunction:适合基本类型(支持compare方法)

#pragma mark SORT METHOTDS

NSInteger sortObjectsByLatestTime(id obj1,id obj2, void *context)

{

 

NSDate* d1 = [(MessageGroup*)obj1latestTime];

NSDate* d2 = [(MessageGroup*)obj2latestTime];

 

//sort by desc

return [d2compare:d1];

}

 

NSInteger dateSort(id obj1,id obj2, void *context)

{

NSDate* d1 = ((Inbox*)obj1).datetime;

NSDate* d2 = ((Inbox*)obj2).datetime;

 

 

return [d1compare:d2];

}

////////////////////////////////////////////////////////////////////

 

 

-(NSArray*)sortedMessages

{

return [[groupMessagesallValuessortedArrayUsingFunction:sortObjectsByLatestTimecontext:NULL];

}

//////////////////////////////////////////////////////////

sortUsingDescriptors:适合元素是dict类型,initWithKey既是dict key.

 

NSMutableArray *regions = [NSMutableArrayarray];

 

 

NSSortDescriptor *sortDescriptor = [[NSSortDescriptorallocinitWithKey:@"name"ascending:YES];

NSArray *sortDescriptors = [NSArrayarrayWithObject:sortDescriptor];

[regionssortUsingDescriptors:sortDescriptors];

[sortDescriptor release];

 

 

分享到:
评论

相关推荐

    Objective—C中的排序

    在Objective-C中,排序通常涉及到`NSArray`或`NSMutableArray`对象,因为它们提供了对元素的存储和操作。数组对象可以包含各种类型的对象,包括数字(整型或浮点型)、字符串和自定义对象。 对于数字的排序,...

    Objective-C中NSArray的基本用法示例

    1. **NSArray排序1**: 使用`sortedArrayUsingSelector:`方法,传入`compare:`作为参数。这个方法会根据对象的内置比较机制对数组进行排序。在我们的例子中,数组元素是字符串,所以`compare:`方法会按照字典顺序对...

    oc数组排序

    NSArray *sortedArray = [mutableArray sortedArrayUsingSelector:@selector(compareByAge:)]; ``` 这种方法适用于对象自身具备比较逻辑的情况。 总的来说,Objective-C提供了灵活的数组排序机制,可以根据实际需求...

    iphone数组学习

    7. **- (NSArray *)sortedArrayUsingSelector:(SEL)aSelector;** - 使用指定的选择器对数组进行排序,通常选择器应该是某个对象实例方法的名称,该方法应该返回两个对象之间的比较结果。 8. **- (id)lastObject;**...

    ios-防微信索引动画,数据模型排序.zip

    1. **本地排序**:在数据加载到内存后,使用`NSArray`的`sortedArrayUsingComparator:`或`sortedArrayUsingSelector:`方法,或者`NSMutableArray`的`sortUsingComparator:`或`sortUsingSelector:`方法对数据进行排序...

    iOS开发中的几种设计模式介绍

    例如,NSArray的`sortedArrayUsingSelector:`可以实现不同的排序策略。策略模式遵循接口隔离原则,鼓励使用组合而非继承,以保持代码的灵活性和可扩展性。经典的鸭子示例展示了如何通过组合行为来实现多种鸭子的行为...

    斯坦福大学开放课程:iOS开发教程2010年秋(Lecture 4)

    - `-(NSArray*)sortedArrayUsingSelector:(SEL)aSelector;`:返回一个排序后的数组。 - `-(id)lastObject;`:返回数组中的最后一个对象(如果没有对象则返回nil)。 - **NSMutableArray**:这是NSArray的可变版本...

    iOS实现联系人按照首字母进行排序的实例

    这里没有提供具体实现,但通常会使用`NSArray`的`sortedArrayUsingComparator:`或者`sortedArrayUsingSelector:`方法来实现。 最后,对每个分组内的联系人数组进行排序,这可以通过调用`sortedArrayUsingComparator...

    试读样章 第三章

    此外,`NSArray`和`NSMutableArray`还提供了排序方法,如`sortedArrayUsingSelector:`可以按照指定的选择器对数组进行排序。 #### 3.4 通过不同方式遍历数组 遍历数组可以使用多种方法,包括但不限于传统的`for`...

    IOS开发中的设计模式汇总

    在Objective-C中,比如NSArray的sortedArrayUsingSelector方法,可以根据传入的比较选择器动态改变排序策略。策略模式有助于保持代码的灵活性和可扩展性,避免硬编码特定行为。 6. **工厂模式**: 工厂模式是一种...

Global site tag (gtag.js) - Google Analytics