This is confusing to me. I have a function that does this:
void ListAllStoredLocations(NSString *SearchTerm){
NSMutableDictionary *item;
NSString* filePath = [[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingString:@"/Preferences/yourprogram.plist"];
item = [[[NSMutableDictionary alloc] initWithContentsOfFile:filePath] mutableCopy];
NSMutableArray *ReadStoredArray = [item objectForKey:SearchTerm];
NSMutableArray *SortedArray = [[NSMutableArray alloc] init];
NSString *CurrentResult=@"";
for (int i = 0; i< [ReadStoredArray count]; i++){
CurrentResult=(NSString *)[ReadStoredArray objectAtIndex:i];
[SortedArray addObject:CurrentResult];
}
[SortedArray sortUsingSelector:@selector(compare:)];
for (int i = 0; i< [SortedArray count]; i++){
NSLog(@"%@",[SortedArray objectAtIndex:i]);
}
[item release];
}
Which finds outputs NSStrings in the first for loop like this:
Location1
Location2
Not a location
Location2
Location3
Location2
and I want the output to be alphabetical:
Location1
Location2
Location2
Location2
Location3
Not a location
But, no matter what, the "[SortedArray sortUsingSelector:@selector(compare:)];" just does not sort the array. Nothing happens.
Maybe I'm going about this all wrong, but every example that I've seen online sorts NSStrings like this - so I don't know what to do.
My endgame, if there is a better solution out there is to output the count of the greatest repeated entry. I was thinking that sorting would be a step in that direction.
Really, what I'm looking for is this as output:
Location2
Because "location2" has the most duplications in that list.
Any help?
Given your array of string looks like this:
NSMutableArray * array = [NSMutableArray array];
[array addObject:@"Location1"];
[array addObject:@"Location2"];
[array addObject:@"Not a location"];
[array addObject:@"Location2"];
[array addObject:@"Location3"];
[array addObject:@"Location2"];
NSLog(@"------------- original:");
for (id obj in array) NSLog(@"%@", obj);
You can sort it like this:
NSLog(@"------------- sorted:");
NSArray * sortedArray =
[array sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
for (id obj in sortedArray) NSLog(@"%@", obj);
Output:
2010-02-06 00:24:14.915 x[23867:903] ------------- original:
2010-02-06 00:24:14.917 x[23867:903] Location1
2010-02-06 00:24:14.921 x[23867:903] Location2
2010-02-06 00:24:14.922 x[23867:903] Not a location
2010-02-06 00:24:14.922 x[23867:903] Location2
2010-02-06 00:24:14.923 x[23867:903] Location3
2010-02-06 00:24:14.924 x[23867:903] Location2
2010-02-06 00:24:14.924 x[23867:903] ------------- sorted:
2010-02-06 00:24:14.925 x[23867:903] Location1
2010-02-06 00:24:14.926 x[23867:903] Location2
2010-02-06 00:24:14.926 x[23867:903] Location2
2010-02-06 00:24:14.927 x[23867:903] Location2
2010-02-06 00:24:14.927 x[23867:903] Location3
2010-02-06 00:24:14.928 x[23867:903] Not a location
If you want to find the object with the most occurrences, given the original array:
NSCountedSet * set = [[NSCountedSet alloc] initWithArray:array];
for (id obj in set) NSLog(@"%d - %@", [set countForObject:obj], obj);
int count = 0;
int maxc = 0;
id maxobj;
for (id obj in set)
{
count = [set countForObject:obj];
if (maxc < count) maxc = count, maxobj = obj;
}
NSLog(@"max is: %d - %@", maxc, maxobj);
Output:
2010-02-06 00:39:46.310 x[24516:903] 1 - Location1
2010-02-06 00:39:46.311 x[24516:903] 1 - Not a location
2010-02-06 00:39:46.311 x[24516:903] 3 - Location2
2010-02-06 00:39:46.312 x[24516:903] 1 - Location3
2010-02-06 00:39:46.313 x[24516:903] max is: 3 - Location2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With