NSMutuableArray sort problem - iphone

NSMutableArray *labels;
and it is properly populated.
NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: #"self" ascending: NO];
labels = [labels sortedArrayUsingDescriptors: [NSArray arrayWithObject: sortOrder]];
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI addObject:]: unrecognized selector sent to instance.
Even if I change NSArray to NSMutuableArray in the last line, I still get the error.
Thanks

dont assign the same array to it.if it is nsarray which is immutable.
so do like this
put ur
labels as NSMutableArray
NSMutableArray *resultArray;
NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: #"self" ascending: NO];
resultArray = [[labels sortedArrayUsingDescriptors: [NSArray arrayWithObject:sortOrder]] mutableCopy];
self.labels=resultArray;//updated code
NSLog(#"resultArray : %# \n\n",resultArray);

NSArray * descriptors = [NSArray arrayWithObjects: sortOrder, nil];
labels = [labels sortedArrayUsingDescriptors:descriptors];
This should do the trick for you.

Related

sorting UITableview cells by creation order swift

Right now I have a tableview that loads data from coredata and I would like to know how it is possible to order these cells by their order of creation. I'd like for the user to be able to see the most recent cells created at the top of the tableview.
Thanks.
You can:
Add a sort descriptor to your request:
//Entity
NSString *entityName = #"Days";
//Fetch Request
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:entityName];
//Sort
request.sortDescriptors = #[[NSSortDescriptor sortDescriptorWithKey:#"created_at" ascending:YES]];
Sort the array of results:
NSSortDescriptor * sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"DatePropertyName" ascending:YES];
NSArray *descriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray *sortedArray = [myArray sortedArrayUsingDescriptors:descriptors];

Core Data - Possible to sort TableView using blocks in NSSortDescriptor?

I am trying to sort a table by an author's last name. Currently, the data is saved as a string in core data as "FirstName LastName."
I understand that custom selectors in core data are out of the scope of Cocoa and return an 'NSInvalidArgumentException', reason: 'unsupported NSSortDescriptor selector: compareToLastName:' Is this true for using a comparator as well?
Here is what I am working from that seems to ignore the comparator completely.
// Sort the request
NSSortDescriptor *sortByType;
if ([sort isEqualToString:#"author"]) {
NSComparator comparisonBlock = ^(id obj1,id obj2) {
NSString *obj1B = [[(NSString *)obj1 componentsSeparatedByString:#" "] lastObject];
NSString *obj2B = [[(NSString *)obj2 componentsSeparatedByString:#" "] lastObject];
return (NSComparisonResult) [obj1B compare: obj2B];
};
sortByType = [[NSSortDescriptor alloc] initWithKey:sort ascending:YES comparator:comparisonBlock];
}
else {
sortByType = [[NSSortDescriptor alloc] initWithKey:sort ascending:YES];
}
NSSortDescriptor *secondSort = [[NSSortDescriptor alloc] initWithKey:#"title" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortByType, secondSort, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
Thanks!
Yes this is even true for NSComparator since the sort descriptors are compiled and executed in SQL itself for performance reasons it isn't really supported.
You can read more here https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/CoreData/Articles/cdPersistentStores.html#//apple_ref/doc/uid/TP40002875-SW11 under Fetch Predicates and Sort Descriptors.

initWithArray doesn't work with NSArray property

In viewDidLoad, I have:
NSArray *selectedPeople = [[selectedObject people] allObjects];
NSArray *peeps = [[NSArray alloc] initWithArray:selectedPeople];
This works just fine, but when I use an NSArray that was declared in my properties it doesn't:
NSArray *selectedPeople = [[selectedObject people] allObjects];
people = [[NSArray alloc] initWithArray:selectedPeople];
The program crashes and says that:
[People isEqualToString:]: unrecognized selector sent to instance 0x6031910
I'm using CoreData and selectedPeople holds People objects. I'm not sure what I'm missing here.
I'm pretty sure Core Data setters for a collection of objects take a set and not an array.
You could try the following:
self.people = [[NSSet setWithArray:selectedPeople];
Is people one of your properties? Did you mean to do:
self.people = [[NSArray ...
instead?

How to avoid "NSInternalInconsistencyException" in iPhone?

I am sorting an mutable array. For sorting I use:
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:#"pubDate" ascending:NO];
[recent sortUsingDescriptors:[NSArray arrayWithObjects:descriptor, nil]];
recent1 = [recent sortedArrayUsingDescriptors:descriptor];
[descriptor release];
I am getting this error:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '*** -[NSCFArray replaceObjectAtIndex:withObject:]: mutating method sent to immutable object'
The line
recent1 = [recent sortedArrayUsingDescriptors:descriptor];
show warnings
"passing argument 1 of 'sortedarrayusingdescritors' from distinct objective c type " and
"assignment from distinct objective c type"
In my code, both recent and recent1 are NSMutable arrays. Where do I go wrong?
recent1 = [recent sortedArrayUsingDescriptors:descriptor];
must be:
recent1 = [recent sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor, nil]];
Though I have no idea why you would want to sort an array that you already sorted with the same sort descriptors on the line directly above.

Sorting an Array of custom objects by a dictionary included in the custom object: How?

I have an array of custom objects. The objects includes a dictionary.
Something like this:
CustomDataModel *dataModel;
dataModel.NSString
dataModel.NSDictionary
dataModel.image
I'd like to sort by one of the objects in the dictionary:
dataModel.NSDictionary ObjectWithkey=#"Name"
The dataModel gets loaded into an NSArray. I now want to sort the by the #"Name" key in the dictionary. Is this something NSSortDescriptor can handle? Basic sort works fine, just haven't figured this one out yet...
Your question isn't completely clear to me, but you can try something like this on your NSArray:
- (NSArray *)sortedItems:(NSArray*)items;
{
NSSortDescriptor *sortNameDescriptor =
[[[NSSortDescriptor alloc]
initWithKey:#"Name" ascending:NO]
autorelease];
NSArray *sortDescriptors =
[[[NSArray alloc]
initWithObjects:sortNameDescriptor, nil]
autorelease];
return [items sortedArrayUsingDescriptors:sortDescriptors];
}
//Sort an array which holds different dictionries - STRING BASED - Declare it in the .h
- (NSArray *)sortStringsBasedOnTheGivenField:(id)dictionaryKey arrayToSort:(NSMutableArray *)arrayToHoldTemp ascending:(BOOL)ascending {
NSSortDescriptor *nameDescriptor = [[NSSortDescriptor alloc] initWithKey:dictionaryKey ascending:ascending selector:#selector(localizedCaseInsensitiveCompare:)] ;
NSArray *descriptors = [NSArray arrayWithObject:nameDescriptor];
[arrayToHoldTemp sortUsingDescriptors:descriptors];
[nameDescriptor release];
return arrayToHoldTemp;
}
Usage:
self.mainArrayForData = [NSArray arrayWithArray:[self sortNumbersBasedOnTheGivenField:#"Name" arrayToSort:arrayWhichContainsYourDictionries ascending:YES]];
the above method is good for an array that holds dictionaries