Showing posts with label compare. Show all posts
Showing posts with label compare. Show all posts

Thursday, March 22, 2012

How to sort an NSArray with integers elements ?

 Its very very simple.



NSMutableArray *heightArray=[[NSMutableArray alloc]init];
for(int i=0; i<10; i++)
{
     float value=(rand()%100.0)/10;
     [heightArray addObject:[NSNumber numberWithFloat:value]];
}
NSArray *sortedArray = [heightArray sortedArrayUsingSelector:@selector(compare:)];
//Lets print the sorted Array
NSLog(@"sortedArray=%@",sortedArray);



For sorting an array which contains  instance of custom class. You can check this old post.



Tuesday, April 5, 2011

Difference between NSString: isEqualTo and NSString: compare

isEqualTo is used for comparing the value of string.

for e.g.

NSString *a=@"Reetu";
NSString *b=@"Mike";
NSString *c=@"Mike";


if([a isEqualTo:b]) -> It will return No;
if([c isEqualTo:b]) -> It will return YES;

compare is used comparing(generally for sorting the string) by lexical order(in dictionary which string will come first).

if([a compare:b]) -> It will return 1;
if([b compare:a]) -> It will return -1;