This question already has answers here:
How do I sort an NSMutableArray with custom objects in it?
(27 answers)
Closed 8 years ago.
My object "Event" has got a property NSDate.
I fill a NSMutableArray with "Event" objects.
Is there a way to order by NSDate the members of my NSMutableArray?
Thank you all.
Assuming you have an NSMutableArray
[yourArray sortUsingComparator:^NSComparisonResult(Event *obj1, Event *obj2){
return [obj1.date compare:obj2.date];
}];
Otherwise :
NSArray sortedArray = [yourArray sortedArrayUsingComparator:^NSComparisonResult(Event *obj1, Event *obj2) {
return [obj1.date compare:obj2.date];
}];
Related
This question already has an answer here:
Getting an array of a property from an object array
(1 answer)
Closed 7 years ago.
Is it possible to take an NSArray of custom objects and get an array of values from the objects in the array?
So if i had a class
#interface CustomObject : NSObject
{
NSNumber *number;
NSString *studentName;
}
And an NSArray of n+ CustomObject
Is it possible to take the Array and get an NSArray of the just the NSNumber number values?
So
NSArray : [
NSNumber,
NSNumber,
NSNumber
]
Yes, you can do this with the KVO method valueForKey:
NSArray *numbers = [myArray valueForKey:#"number"];
You can check out the method in the NSArray class reference.
Yes you can do that. You can use valueForKey: KVC method for getting the extracting the number from the object contained in the array.
NSArray *allNumbers = [yourArray valueForKey:#"number"];
You can read more about valueForKey: in NSKeyValueCoding Class Reference
This question already has answers here:
How do I sort an NSMutableArray with custom objects in it?
(27 answers)
Closed 8 years ago.
I have a NSArray with core data entities called:
"Struct"
thy have an attribute called "value" that can be a double
Struct *struct = [array objectAtIndex:0];
double val = [struct value];
I have to sorting this array by this value but I don't understand what's the way to do it, because I have not a key to access to the object.
Do you have any suggests?
NSMutableArray *sortedArray;
sortedArray = [array sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
NSNumber *s1 = #([(Struct *)a value]);
NSNumber *s2 = #([(Struct *)b value]);
return [s1 compare:s2];
}];
This question already has answers here:
How do I sort an NSMutableArray with custom objects in it?
(27 answers)
Closed 9 years ago.
Hopefully someone can help.
I'm adding multiple objects to a NSMutableArray and I need to sort the order based on the first element which will always be a number.
However I'm unsure how to do this?
For example:
NSMutableArray *array = [[NSMutableArray alloc] init];
NSArray *object = [NSArray arrayWithObjects: #"1",#"Test",#"Test"];
[array addObject:object];
Thanks
If your array always contains other arrays, and the first element of the innermost array is always a string containing a number, you could use the NSMutableArray method sortUsingComparator to sort your array:
[array sortUsingComparator: ^(NSArray* obj1, NSArray* obj2)
{
int value1 = [obj1[0] integerValue];
int value2 = [obj2[0] integerValue];
if (value1==value2)
return NSOrderedSame;
else if (value1 < value2)
return NSOrderedAscending;
else
return NSOrderedDescending;
}
];
In the sortUsingComparator family of methods, you supply a block of code that the sort method uses to compare pairs of objects in your array. The block uses the standard typedef NSComparator, which takes 2 objects as parameters and returns a value of type NSComparisonResult.
The code above will probably crash if all the objects in your array are not arrays of strings. (Actually it would work if the first element of each component array was an NSNumber, since NSNumber also responds to the integerValue message.)
If you are going to use this code in a very controlled environment where you can be sure that the data you are sorting is well-formed, it should work as written. If there is any chance that the objects in the array would be of a different type, or be empty, or that their first element would not respond to the integerValue messages, then you should add error checking code.
If you sort your array alphanumerically, the object #"1" will appear before any words. Keep in mind though that #"1" in your code above is a string, not a number.
As to how to sort an array, look into [NSArray sortedArrayUsingComparator:] and similar methods.
This question already has answers here:
How to sort a NSArray alphabetically?
(7 answers)
Closed 9 years ago.
I am new to objective-c, ios.
I'm trying to sort in alfabetic order a NSMutableArray called filteredList that contains objects of type NSString.
so if my mutable array contains : [Mary, Bill, John] I would like to have [Bill, Mary, John]
I did the following:
[filteredList sortUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
but I do not see any change. I did read and tried other solutions like compare: instead of localizedCaseInsensitiveCompare but still nothing.
Updated
NSArray *sortedArray =[unSortedArray sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];//unSortedArray is NSMutableArray
unSortedArray = [[NSMutableArray alloc]initWithArray:sortedArray];
I would do something like:
NSArray *sortedArray = [filteredList sortedArrayUsingComparator:^(NSString *str1, NSString *str2) {
return (NSComparisonResult)[str1 compare:str2];
}];
filteredList = [sortedArray mutableCopy];
Please refer the below code:-
filteredList = (NSMutableArray*)[filteredList sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
This question already has answers here:
Filter NSArray of custom objects
(3 answers)
Closed 9 years ago.
I have NSMutableArray which contains a list of people , now I need to get a list of all people where gender = male, how can I do that? should I get into NSPredicates to do that?
Copy this NSArray category to to somewhere in your code
#implementation NSArray (My)
-(NSArray*)arrayWithPredicate:(BOOL(^)(id obj))predicate {
NSMutableArray* objs = [NSMutableArray array];
[self enumerateObjectsUsingBlock:^(id o, NSUInteger idx, BOOL *stop) {
if (predicate(o)) {
[objs addObject:o];
}
}];
return objs;
}
#end
Then where you need to get the male ones:
NSArray* males = [people arrayWithPredicate:^BOOL(id obj) {
// Gender check
}];
The advantage over NSPredicate is that you don't have to use a literal string to specify the criteria (quite a mess if the criteria is complex).
yes you can use like this,
NSArray *filtered = [data filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"self.gender == %#", #"male"]];
NSLog(#"%#",filtered);