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:)];
Related
This question already has answers here:
Having trouble adding objects to NSMutableArray in Objective C
(2 answers)
Closed 7 years ago.
I am trying to store an NSString variable in an NSMutableArray:
#property(weak,nonatomic) NSMutableArray * stringLines;
//----
NSString * string=#"hello";
[self.stringLines addObject: string];
int i=0;
for (id obj in _stringLines){
NSLog(#"printing labels: index %x word %#",i, obj);
i++;
}
but when I try to print the mutable array, it shows me null values. I don't want to assign the text directly to the array, it has to be through the variable string.
Did you initialize self.stringLines? It may be nil. Try initializing to an empty array, then add the strings.
#property(weak,nonatomic) NSMutableArray * stringLines;
//----
self.stringLines = [NSMutableArray array];
NSString *string = #"hello";
[self.stringLines addObject:string];
NSLog(#"Print strings array: %#", self.stringLines);
That's because you have not allocated the array object. So if you add anything to a nil object you won't find anything inside it. Use this code
//----
self.stringLines = [[NSMutableArray alloc] init];
NSString * string=#"hello";
[self.stringLines addObject: string];
you can also initialize the array by adding the object with literal, so you need only 1 line of code instead of 3:
self.stringLines = #[#"hello"];
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];
}];
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:
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);
This question already has answers here:
The best way to remove duplicate values from NSMutableArray in Objective-C?
(14 answers)
Closed 9 years ago.
i have a nsmutablearray as is shown below. I would like to retrieve list value without redundancy. in this example i should retrive 10 20 30
NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:#"10",#"20",#"30",#"30",#"30",#"30",#"20", nil];
Transform the array into a set, and then back into an array.
NSSet *set = [NSSet setWithArray:array];
NSArray *a = [set allObjects];
You can also have this new array sorted:
NSArray *a2 = [a sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [obj1 compare:obj2];
}];
Since iOS 5 you can use -[NSOrderedSet orderedSetWithArray:] which preserves the order:
NSArray *a2 = [[NSOrderedSet orderedSetWithArray:array] array];
NSArray *withRedunecy = [[NSSet setWithArray: array] allObjects];
This will be the one way like you can create new NSArray which has no duplicate objects or you need to create a logic for getting unique objects like insert one by one with checking is it already present or not.
Try to use this on
NSArray *array = #[#"10",#"20",#"30",#"30",#"30",#"30",#"20"];
NSArray *newArray = [[NSSet setWithArray:array] allObjects];
NSLog(#"%#", newArray);
Output :
(
30,
20,
10
)
Only this line of code will work fine .
NSSet *mySet = [NSSet setWithArray:array];
now mySet will have unique elements.so create array with this set
NSArray *myarray = [mySet allObjects];