To start, you lose your memory like crazy. Learn the rules: if you create an object (alloc / init or copy), you own it and must free it.
item = [[[NSMutableDictionary alloc] initWithContentsOfFile:filePath] mutableCopy];
On this line, you create a mutable dictionary, and then create a mutable copy, losing the original instance. You should replace this as follows:
item = [[NSDictionary alloc] initWithContentsOfFile:filePath];
In fact, you do not modify the dictionary in your code, so I assume it is NSDictionary.
Next, the type of this declaration:
NSMutableArray *ReadStoredArray = [item objectForKey:SearchTerm];
wrong. Even if the dictionary was changed, its members are not guaranteed. ( mutableCopy is a shallow copy.) Since you are not actually mutableCopy this array, change the line to:
NSArray *ReadStoredArray = [item objectForKey:SearchTerm];
Now, if you want to copy elements from ReadStoredArray to SortedArray, you can replace the loop with
[SortedArray addObjectsFromArray:ReadStoredArray]
But since you are making an exact copy, you can also just write
SortedArray = [ReadStoredArray mutableCopy];
But you don't need the SortedArray change, so you can just call this other form, which returns a new sorted array, rather than sorting the changed array in place:
SortedArray = [ReadStoredArray sortedArrayUsingSelector:@selector(compare:)];
So now your function looks like this:
void ListAllStoredLocations(NSString *SearchTerm) { NSDictionary *item; NSString* filePath = [[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingString:@"/Preferences/yourprogram.plist"]; item = [[NSDictionary alloc] initWithContentsOfFile:filePath]; NSArray *ReadStoredArray = [item objectForKey:SearchTerm]; NSArray *SortedArray = [ReadStoredArray sortedArrayUsingSelector:@selector(compare:)]; for (int i = 0; i< [SortedArray count]; i++){ NSLog(@"%@",[SortedArray objectAtIndex:i]); } [item release]; }
You do not need to issue ReadStoredArray or SortedArray because you do not own them (no / init or copy is highlighted in calls).
As for your actual question ... there is no obvious reason from the code why sorting will not work. Sorry Many common problems would cause exceptions rather than silence.
If the file does not exist or cannot be loaded, initWithContentsOfFile: would throw an exception. If ReadStoredArray was nil, then CurrentResult would be nil, and addObject: would throw an exception. If the objects in the array did not respond to the compare: selector, sortUsingSelector: would throw an exception. If the SortedArray was zero, the code will fail, but it will also not output the result. (Also, in order for this to be nil, alloc / init would have to fail, meaning you did not have enough memory.)
Besides the memory leaks and the unconventional style (starting your variable names with lowercase letters), there is nothing wrong with the code. Something is missing.