开发者

Better ways of sorting NSArray in Objective - C [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

G'Day Everyone,

Recently I have start开发者_JS百科ed learning iOS development. I initiated my learning process by learning Objective C language. Before I am proficient in Java, In natural computer sense "Java is my first language".

Because of that I am having lots of difficulty when I see really long methods in Obj - C, Which meant to do really easy things. Let me share some codes which are very long just to sort NSArray content Ascending order and Descending order (Alphabetically)

Sort NSArray in Ascending order A - Z


[dataToShow sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; // dataToShow is my NSArray

Sort NSArray in Descending order Z - A


NSSortDescriptor* sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:nil ascending:NO selector:@selector(localizedCompare:)];
[dataToShow sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]; // dataToShow is my NSArray

Now is there any better way, By better way I mean shorter way of achieving same result? As I am naive, I would like to know if there is any other method which might be as long as above one but does sorting in more better ways.

Thank you


Get used to it. It's common and even desirable in Objective-C to have long, descriptive method names. Of course you could wrap things that you do very often in your own methods or macros, but I wouldn't deviate too much from the Cocoa naming style as it makes your code harder to read for others.

See the Coding Guidelines for Cocoa for some background.


I suggest you get comfortable with blocks.

NSArray* sortedArray =[array sortedArrayUsingComparator: ^(id thing1, id thing2) 
{ 
    /* return NSComparisonResult following conventions of strcmp */ 
}];

It doesn't get much easier than that

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜