Suppose I have an array
having elements
"am","john","rosa","freedom"
. I want to compare
these elements and result would be the word
and the size of the longest word
. I am using objective C
.
Asked
Active
Viewed 642 times
0

user12345
- 2,400
- 8
- 33
- 40
-
possible duplicate? http://stackoverflow.com/questions/1137839/comparing-arrays-in-objective-c – Adnan Aug 31 '10 at 15:03
-
Could you precise : what kind of array (C style array or NSArray) ? Rem :comparing two element always return a single value (NSOrderDescending, NSOrderAscending or NSOrderedSame). – VdesmedT Aug 31 '10 at 15:18
-
Are `elements` instances of `NSString` or some other class? – falconcreek Aug 31 '10 at 15:34
3 Answers
2
There isn't a "built-in" way of doing this, however you can use NSArray
's sortedArrayUsingSelector:
and create a category on NSString
to provide a lengthCompare:
method.
// NSString+LengthCompare.h
#import NSString.h
@interface NSString (LengthComparison)
- (NSComparisonResult)lengthCompare:(NSString *)aString;
@end
// NSString+LengthCompare.m
#import NSString+LengthCompare.h
@implememtation NSString (LengthComparison)
- (NSComparisonResult)lengthCompare:(NSString *)aString
{
if ([self length] < [aString length]) {
return NSOrderedAscending;
} else if ([self length] > [aString length]) {
return NSOrderedDescending;
} else {
return NSOrderedSame;
}
}
@end
Now you can sort an of strings in ascending order using lengthCompare:
NSArray *array = [NSArray arrayWithObjects: @"am", @"john", @"rosa", @"freedom", nil];
NSArray *arraySortedByStringLength = [array sortedArrayUsingSelector:@selector(lengthCompare:)];
NString *shortestWord = [[arraySortedByStringLength objectAtIndex:0] retain];
NSLog(@"shortest word, %@ has length %d", shortestWord, [shortestWord length];
[shortestWord release];
NString *longestWord = [[arraySortedByStringLength lastObject] retain];
NSLog(@"Longest word, %@ has length %d", longestWord, [longestWord length];
[longestWord release];

falconcreek
- 4,170
- 1
- 21
- 22
-
1If you're only going to do it once, you could also use -[NSArray sortedArrayUsingComparator:], which uses a block instead of a selector. Then you don't have to declare a method that only gets called once. – Alex Martini Aug 31 '10 at 16:29
-
-
tag is [iphone]. on the Mac, blocks were introduced in 10.6 (Snow Leopard) – falconcreek Aug 31 '10 at 17:59
-
updated code to correctly select the longest word given the sortedArrayUsingSelector: returns an array sorted in *ascending* order – falconcreek Sep 01 '10 at 16:16
0
Sounds like a classical logic exercise or is it something I miss in your question ?
int longestWordIndex = 0;
NSUInteger longestWordSize = 0;
for (int i=0 ; i<[nameArray count] ; i++) {
NSString* element = (NSString*)[nameArray objectAtindex:i];
if([element lenght] > longestWordSize) {
longestWordSize = [element lenght];
longestWordIndex = i;
}
}
NSLog("Longest word is %@ with size of :%d", [nameArray objectAtIndex:longestWordIndex], longestWordSize);

VdesmedT
- 9,037
- 3
- 34
- 50
-
well thanx,I know this solution. But this is a lay man solution. If you can tell me any inbuilt methods which is available in objecive C. – user12345 Aug 31 '10 at 15:29
-
Yep, I thought so. You could probably mess up with indexesOfObjectsPassingTest but you will have to call it twice anyway. No other idea – VdesmedT Aug 31 '10 at 15:52
0
I'll add one more approach to the two above -- use a block to do the body of your iteration.
__block NSUInteger longestWordSize = -1; // Make sure at least one object will be longer.
__block NSUInteger longestWordIndex;
[nameArray enumerateObjectsUsingBlock:^(id currentWord, NSUInteger index, BOOL *stop) {
if ([currentWord length] > longestWordSize) {
longestWordSize = [currentWord length];
longestWordIndex = index;
}
}];
NSLog("Longest word is %@ with size of :%d", [nameArray objectAtIndex:longestWordIndex], longestWordSize);
Edit: The max and index have to be of storage type __block
so they can be changed from inside the block.

Alex Martini
- 750
- 4
- 11