Sort an array of NSDictionary based on dates [duplicate] - objective-c

This question already has answers here:
How can I sort an NSArray containing NSDictionaries?
(3 answers)
Closed 10 years ago.
I'm looking for a way to sort an NSMutableArray of dictionaries based on dates contained in the dictionaries. Each one looks like this:
{
name: tom,
surname: smith,
date of birth (NSDate): 2013-02-21 15:25:27
}
I know of the methods earlierDate etc., but since they only compare two dates I'm just a bit stuck on how to iterate through the array so that I come out with a fully ordered array of dictionaries?

using sortUsingComparator this can be solved easily:
[yourArray sortUsingComparator:^NSComparisonResult(NSDictionary *a, NSDictionary *b) {
return [a[#"dateOfBirth"] compare:b[#"dateOfBirth"]];
}];

Related

Randomly select from an array of strings in Objective-C? [duplicate]

This question already has answers here:
Picking a Random Object in an NSArray
(8 answers)
Closed 3 years ago.
I have done some digging, and I don't believe this question has been answered yet (though I would imagine it would be simple).
I want to select a random string from an array. For example:
NSArray *strings = #[#"String1", #"String2", #"String3"];
NSString *randomString = // how to randomly select one of the three strings in the array?
You want arc4random_uniform():
NSString *randomString = strings[arc4random_uniform(strings.count)];
More info in this SO answer.

Sort other arrays based on one array (Obj-C) [duplicate]

This question already has answers here:
Sorting two NSArrays together side by side
(4 answers)
Order two NSMutableArrays based on one
(2 answers)
Closed 5 years ago.
I have three NSMutableArray's:
NSMutableArray *dateArray;
NSMutableArray *nameArray;
NSMutableArray *imageArray
I would like to sort the dateArray based on the closest date, and then have the nameArray and imageArray bases on this new sorted order of the dateArray.
How can I accomplish this without changing the structure that I've already setup above?
Without considering why you have three arrays here is an algorithm to "sort" them together:
Create a new array the same size as your three arrays filled with integers starting from 0[†].
"Sort" this new array using sortedArrayUsingComparator: passing a block to the comparison.
Your block will get called with two values from the array, being two integers. Use those integers to index into dataArray and compare the values you find there, returning the result as the block result.
After this you will have an array of integers you can use to indirectly index into your three arrays, e.g. something like:
NSArray *sortedIndicies = [arrayOfInts sortedArrayUsingComparator:...];
// first item according to the sort
id firstDate = dateArray[sortedIndicies[0]];
id firstName = nameArray[sortedIndicies[0]];
You can of course use sortedIndicies to reorder your original 3 arrays if required.
There are other ways to achieve what you require, for example you can "zip" your three arrays into a single array of three items, sort, then "unzip" again. Zip operations are not provided by NSArray so you need to write them (both are a simple loop).
HTH
[†] You can use a "fake" array for this is you wish, for inspiration see this answer - your implementation could be simpler.

Searching an object (by an attribute) in a NSMutableArray [duplicate]

This question already has answers here:
Finding a particular instance in an array by a property
(5 answers)
Closed 8 years ago.
I need to get a better way to find an object in a NSMutableArray.
At the moment i do it this way:
for(classOfTheObject *thisItem in arrayOfObjects){
if(thisItem.foreign_key == serchThisObject.foreign_key){
// found it
}
}
but this is a very bad way i think. Is it possible to get the object without a for loop?
In an array it will always require some type of loop/enumaration to actually find it. If foreign_key is the only search/identification criteria that you use then you may consider using an NSDictionary with the value of foreign_key as key.
If I were you,I would use NSPredicate :
NSPredicate *applePred = [NSPredicate predicateWithFormat:
#"employer.name == 'Apple'"];
NSArray *appleEmployees = [people filteredArrayUsingPredicate:applePred];

Objective-C - sort array of strings based on string slice [duplicate]

This question already has answers here:
How to sort a NSArray alphabetically?
(7 answers)
How to do a natural sort on an NSArray?
(5 answers)
Closed 9 years ago.
NSMutableArray looks like ("xxx_20", "xxx_18", "xxx_16", "xxx_19", "xxx_17")
I want to sort it into ("xxx_16", "xxx_17", "xxx_18", "xxx_19", "xxx_20")
I've tried to use selectors, but I can't even get proper syntax. Does anyone know how to do this properly?
Try this,
NSMutableArray *myarray = [ NSMutableArray arrayWithObjects:#"xxx_20", #"xxx_18", #"xxx_16", #"xxx_19", #"xxx_17", nil];
NSArray *sortedArray = [myarray sortedArrayUsingSelector:#selector(caseInsensitiveCompare:)];
NSLog(#"%#",sortedArray);
It worked fine at my end.

How to slice NSMutableArray [duplicate]

This question already has an answer here:
How to slice an NSArray in Objective-C?
(1 answer)
Closed 8 years ago.
I have a NSMutableArray and need objects [0:5] only. Is there a simple way to slice? Can I drop all objects after index? Can I copy a sub-array to another NSMutableArray?
Use the instance method - (NSArray *)subarrayWithRange:(NSRange)range.
For example:
NSArray* slicedArray = [wholeArray subarrayWithRange:NSMakeRange(0, 5)];
I see James Bedford already answered how to extract a range of indexes. To delete the objects in a range from an NSMutableArray, you can use [wholeArray removeObjectsInRange:...]. To delete all objects after a particular index, you can create an appropriate range as NSMakeRange(index, wholeArray.count - index).