sortUsingSelector未排序NSStrings数组

And*_*yer 4 sorting macos objective-c nsmutablearray

这对我来说很困惑.我有一个功能,这样做:

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];
Run Code Online (Sandbox Code Playgroud)

}

哪个在第一个for循环中找到输出NSStrings,如下所示:

LOCATION1

LOCATION2

不是一个位置

LOCATION2

LOCATION3

LOCATION2

我希望输出是按字母顺序排列的:

LOCATION1

LOCATION2

LOCATION2

LOCATION2

LOCATION3

不是一个位置

但是,无论如何,"[SortedArray sortUsingSelector:@selector(compare :)];" 只是不排序数组.什么都没发生.

也许我说这一切都错了,但我在网上看到的每个例子都是这样对NSStrings进行排序 - 所以我不知道该怎么做.

我的最后阶段,如果有更好的解决方案,那就输出最大重复输入的计数.我在想排序是朝这个方向迈出的一步.

真的,我正在寻找的是输出:

LOCATION2

因为"location2"在该列表中具有最多重复.

有帮助吗?

ste*_*anB 8

鉴于您的字符串数组如下所示:

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);
Run Code Online (Sandbox Code Playgroud)

你可以这样排序:

NSLog(@"------------- sorted:");
NSArray * sortedArray =
     [array sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
for (id obj in sortedArray) NSLog(@"%@", obj);
Run Code Online (Sandbox Code Playgroud)

输出:

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
Run Code Online (Sandbox Code Playgroud)

如果要查找具有最多出​​现次数的对象,则给定原始数组:

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);
Run Code Online (Sandbox Code Playgroud)

输出:

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
Run Code Online (Sandbox Code Playgroud)