Sort NSArray of NSDictionaries - objective-c

I have a problem with sorting... I need to sort NSArray containing NSDictionaries. As an order key I have to use element from the NSDictionary.
I was reading a little about sorting NSArray but it is not clear to me.
Could you try help me?

Use an NSSortDescriptor and a keypath. Assuming the key for the dictionary is key:
NSSortDescriptor *sd = [NSSortDescriptor sortDescriptorWithKey:#"key" ascending:YES];
NSArray *sorted = [array sortedArrayUsingDescriptors:[NSArray arrayWithObject:sd]];

Related

How to fetch data of NSString from array of structure hold by NSMutableArray in objective c?

I have one NSMutableArray tempArray = \[NSMutableArray array\]; which contains an array of structured objects with multiple data: brand_package_id , slide_master_id etc....
I want to sort out the tempArray according to slide_master_id's.
Please see given image and help me to sort this problem.
Try using NSSortDescriptor:
NSSortDescriptor *slideDescriptor = [[NSSortDescriptor alloc]
initWithKey:#"slide_master_id" ascending:YES];
NSArray *sortDescriptors = #[slideDescriptor];
NSArray *sortedArray = [tempArray sortedArrayUsingDescriptors:sortDescriptors];

Sort descriptor not working on nsdate objects in NSArray.

An array contains NSDate objects, how to sort these using sort descriptor ? I am trying the code below:
NSSortDescriptor * dateSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:#"Date" ascending:YES];
NSArray * sortedArray = [allDates addObjectsFromArray:[allDates sortedArrayUsingDescriptors:#[dateSortDescriptor]]];
NSDate conforms to the compare: method and ascending is the default order
NSArray *sortedArray = [allDates sortedArrayUsingSelector:#selector(compare:)];
#Avt, answered this question I don't know why he removed it.
NSSortDescriptor * dateSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:#"timeIntervalSince1970" ascending:YES];
NSArray * sortedArray = [allDates addObjectsFromArray:[allDates sortedArrayUsingDescriptors:#[dateSortDescriptor]]];

Sorting an array of dictionaries in Plist

Trying to sort an array of dictionaries. There are two arrays in the plist file and I am trying to sort one of them and having a lot of trouble. I can extract the dictionaries into a NSArray and sort the files.
I can only do this by extracting the dictionaries so when I try to save the information back to the plist I've lost my structure and other array of dictionaries.
How can I sort the array correctly. I understand I'm extracting the array and then saving it as it's own file. I see what I'm doing wrong but can't figure how the correct way.
NSArray *array = [dict valueForKey:#"Lighting"];
NSSortDescriptor* nameSortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"Controller Zone" ascending:YES];
NSArray* sortedArray = [array sortedArrayUsingDescriptors:[NSArray arrayWithObject:nameSortDescriptor]];
NSString *pathToFileExportSorted = #"/Users/scott/Desktop/plistPreSEsorted.plist";
[sortedArray writeToFile:pathToFileExportSorted atomically: YES];
I have tried something like this but couldn't get that working either
[array sortUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:#"Lighting.Controller Zone" ascending:YES]]];
I just want to sort the Array Lighting by "Controller Zone" and keep the plist as is otherwise.
Thanks for any help.
Original Data:
What I am currently Exporting, does not have Lighting and Shade array. The array is now the only thing in the plist.
it's needed to assign sorted array to Lighting key
...and to save your initial dictionary with changed Lighting key
NSArray *array = [dict valueForKey:#"Lighting"];
NSSortDescriptor *nameSortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"Controller Zone" ascending:YES];
NSArray *sortedArray = [array sortedArrayUsingDescriptors:[NSArray arrayWithObject:nameSortDescriptor]];
NSMutableDictionary *dictToSave = [[dict mutableCopy] autorelease];
[dictToSave setObject:sortedArray forKey:#"Lighting"];
NSString *pathToFileExportSorted = #"/Users/scott/Desktop/plistPreSEsorted.plist";
[dictToSave writeToFile:pathToFileExportSorted atomically:YES];
That should work as you've described.

How to sort an array which contains Dictionaries?

I have an array which contains dictionaries in it; so how can I sort the array according to dictionary key values?
If every element in the array is a dictionary containing a certain key, you can sort the array using a sort descriptor. For instance, if every element is a dictionary containing a key called "name":
NSSortDescriptor *sortByName = [NSSortDescriptor sortDescriptorWithKey:#"name"
ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortByName];
NSArray *sortedArray = [array sortedArrayUsingDescriptors:sortDescriptors];
The other way to achieve this would be using sortedArrayUsingComparator: method
NSArray *sortedArray = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [[obj1 valueForKey:#"value"] compare:[obj2 valueForKey:#"value"]];
}];
why don't you use a sorted dictionary where the property of sorted elements comes already with the data structure?
Please do check it out here
Hope this helps.
The above shared answer by Bavarious helped me.Just want to add one more thing.If you want to sort a mutable array use something like the below code
NSSortDescriptor *sortByName = [NSSortDescriptor sortDescriptorWithKey:#"name" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortByName];
NSMutableArray *sortedArray = [[NSMutableArray sortedArrayUsingDescriptors:sortDescriptors]mutableCopy];
let arrDescriptor = NSSortDescriptor(key: "order", ascending: true)
let sortDescriptors: NSArray = [arrDescriptor]
let sortedArray = instancesubArray.sortedArray(using: sortDescriptors as [AnyObject] as [AnyObject] as! [NSSortDescriptor])
print(sortedArray)

Quicksort to order array by business key?

I've an array of objects, the size of which I cannot predict. The contents of the array are model objects with properties of type nsstring and nsnumber.
I need to sort the array according to one of the properties, an nsnumber. How would you do it in objective-c/Cocoa? Implement quicksort or some other algorithm (which one)? Any libraries that handle that for you?
Update
While the response below is correct, it only works on 10.6 and I'm targeting 10.5.
NSArray has several sorting methods. Given your array, arr,
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:#"numberProperty" ascending:YES];
NSArray *sortedArr = [arr sortedArrayUsingSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
will give you an array sorted (ascending) by #"numberProperty". Obviously, you'll have to substitute the name of the NSNumber property in your model objects for #"numberProperty".
The sorting algorithm is not specified in NSArray's sorting methods.
This is what I came up with:
NSArray *unsortedArray = [results allValues];
NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc]
initWithKey:#"ordinalPosition"
ascending:YES]
autorelease];
NSArray *sortedArray = [unsortedArray
sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
Use qsort() from stdlib.h.