This question already has answers here:
How to sort a NSArray alphabetically?
(7 answers)
Closed 10 years ago.
I want to sort my array alphabetically in objective-c. I have implemented it this way.
//Sorting of the Array
NSArray *sortedArray = [arrName sortedArrayUsingComparator:^(Cars *firstObject, Cars *secondObject) {
return [firstObject.str_name compare:secondObject.str_name];
}];
arrName =[NSMutableArray arrayWithArray:sortedArray];
The problem is all the numbers appear followed by captial letters words followed by lowercase letters items...
I want it to appear alphabetically-> meaning to say that the capital letters and lowercase letters maybe mixed.
Replace compare: with caseInsensitiveCompare:.
Since arrName is mutable, use the 'sortUsingComparator' method instead. It will sort the mutable array in place without creating a new array.
[arrName sortUsingComparator:^(Cars *firstObject, Cars *secondObject) {
return [firstObject.str_name caseInsensitiveCompare:secondObject.str_name];
}];
try this,
NSArray *array=[[NSArray alloc]initWithObjects:#"Object1",#"object1", nil];
array =[array sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
NSLog(#"%#",array);
Related
This question already has answers here:
insert object in an NSMutableArray saved with NSUserDefaults
(6 answers)
Closed 8 years ago.
I have an NSMutableArray and I am trying to use the insertObjectAtIndex method, but it does not work. It seems to think I am trying to do something mutable with an immutable array..... but I am using an NS ..... MUTABLE.... Array. So what an earth could be wrong??
Here is my code:
if ([dataArray count] == 0) {
// Initialise the audio arrays.
dataArray = [[NSMutableArray alloc] init];
// Now add the data to the array.
[dataArray addObject:#"test 1"];
}
else {
[dataArray insertObject:#"test 2" atIndex:0];
}
NSLog(#"d in: %#", dataArray);
The dataArray is defined in the header file like so:
NSMutableArray *dataArray;
Thanks for your time, Dan.
Oh sorry everyone, I just figured out what was wrong. I did edit the code in my question a bit, but basically what I am actually populating the NSMutableArray with is an array stored in an NSUserDefault, (which of course returns immutable arrays only).
So even though I am using an NSMutableArray to store the data, its still has immutable contents. In order to over come this, I ended up using "mutableCopy" iOS method to get a mutable copy of the contents of the NSUserDefaults array.
This question already has answers here:
Having trouble adding objects to NSMutableArray in Objective C
(2 answers)
Closed 8 years ago.
I want to add a first an last name to one index of a mutable array.
I'm loading a first and last name from the AdressBook API and I want the first and last name in an array, so I can display that later to the user.
Currently, I can log the first an last name, but when I try to add these to an array, the array stays null.
This is the code I'm using:
[_names addObject:[NSString stringWithFormat:#"%# %#", firstName, lastName]];
NSLog(#"%#", _names); //prints 'null'
NSLog(#"Name:%# %#", firstName, lastName);//prints 'John Appleseed'
Why isn't my array being updated?
Have you made sure you initiated the array properly using _names = [[NSMutableArray alloc] init]? You cannot add objects into an uninitialized array.
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:
Getting an NSArray of a single attribute from an NSArray
(3 answers)
Closed 9 years ago.
I have the following issue, I need to get an array of values from an array of custom Objects. Is there a method for doing this without iterating the principal array, let me graphic it a little bit.
NSArray *principalArray = #[
customObject1,customObject2,customObject3,....customObject(n)
];
This customObject instances have a properties lets say id,name,lastname.
I want to get an NSArray with the value of name from the principalArray
Thanks for your help.
EDIT:
As somebody pointed out in comments: its a duplicate of existing SO question: Getting an NSArray of a single attribute from an NSArray
There is a method for NSArray - valueForKey - with key being an attribute of your first array. This method returns you an NSArray from an NSArray.
In your case you can do the following:
NSArray *nameArray = [principalArray valueForKey:#"name"];