I have a UITableView each cell shows unique menu item I want items to be sort in my list but those menu items are not coming from an array but from a db object
Example:
cellForRowAtIndexPath{
Menus_Items *menuItem = [menuCategory.hasMenuItems.allObjects objectAtIndex:indexPath.row];
lblMenuItemName.text = menuItem.name;
}
How can I sort menu items ? I could have done it if it was an array i.e.
NSSortDescriptor *orderSort = [[NSSortDescriptor alloc] initWithKey:#"order" ascending:NO];
// String are alphabetized in ascending order
NSSortDescriptor *strNameSort = [[NSSortDescriptor alloc] initWithKey:#"name" ascending:YES];
// Combine the two
NSArray *sortDescriptors = #[orderSort, strNameSort];
arrCategories = [DatabaseHelper getAllData:#"Menus_cat" offset:0 predicate:[NSPredicate predicateWithFormat:#"ofOutlet == %#",outletsObject] sortDescriptor:nil];
// sortedArrayUsingDescriptor is the method use to sort an array which is having multiple parameters in sort descriptor
arrCategories = [arrCategories sortedArrayUsingDescriptors:sortDescriptors];
Help please. Thanks in advance
menuCategory.hasMenuItems.allObjects is an array so you can sort it to get the appropriate item. You could sort it each time or keep a sorted cache of the content. Neither of these options is ideal.
Really, you want to use a fetch request with a sort descriptor, ideally managed by a fetch request controller. Usually you would do that by using the relationship 'backwards'. So, you create the fetch and set the predicate to XXX = %#, where XXX is the relationship name and the supplied parameter is menuCategory.
This is assuming the relationship is 1:many, if it's many:many you will need a different predicate form.
Related
sorry if the title is not clear, i'll explain:
I have a note creation page, after creating a note I'm saving the note using the model class (nsmanagedobject sub class).
I have another table view controller which I'm showing the notes, and I want the notes on the table view to be sorted by creation date, is there a key for that?
this is what i have now:
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"body" ascending:YES];
the key here is the text of the note so its sorted by the abc, can you help me to sort it by date?
tnx
There is no implicit creation date attribute for a managed object. You can create an attribute, say creationDate, in your model and set it to [NSDate date] when you create an object.
Then you can simply sort on this attribute
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"creationDate" ascending:YES];
I am using NSFetchedResultController in my Project. Following is a scenario i want to achieve.
I have a Table Called Contact in which i have have 2 attributes LastMessageDate and ContactName.
I want to sort Contact objects according to LastMessageDate. The objects which don't have LastMessageDate should be sorted according to ContactName (Alphabetically).
For Ex:
Consider i have C1,C2,C3,C4,C5,C6 as My Contact out of Which C2 and C6 have LastMessageDate Present. So the sorted Contacts should be C2,C6,C1,C3,C4,C5
use two sort descriptors first will sort all contact in alphabetic order and second will filter according to last message date. see following code -
NSSortDescriptor *contactName = [[NSSortDescriptor alloc]
initWithKey: #"ContactName" ascending: YES];
NSSortDescriptor *lastMessageDate = [[NSSortDescriptor alloc]
initWithKey: #"LastMessageDate" ascending: YES];
NSArray *sortedArray = [contactArray sortedArrayUsingDescriptors: [NSArray arrayWithObjects: contactName, lastMessageDate, nil]];
Can you set a default date for LastMessageDate, to either distantFuture or distantPast?
Now, when you sort by date these items will be first or last and the second sort descriptor will be used (hence they will be sorted alphabetically).
And your other code would check what the date is and not use when it is equal to distantFuture or distantPast (which could be in a custom accessor method).
I am trying to sort an array of UserWrapper objects. The wrapper object contains the object User, and the User object contains the property UserName (that I want to sort by).
It us easy enough to sort an array of Users (source), but the added layer of UserWrapper complicates things for me. Help please!
Here is my code, which worked for a simple User array:
NSSortDescriptor *nameDescriptor = [[NSSortDescriptor alloc] initWithKey:#"UserName"
ascending:YES
selector:#selector(localizedCaseInsensitiveCompare:)] ;
NSArray *descriptors = [NSArray arrayWithObject:nameDescriptor];
NSMutableArray *contactsStartingWithKey = [nameIndexesDictionary objectForKey:aKey];
[contactsStartingWithKey sortUsingDescriptors:descriptors]; // Exception thrown here because UserName is not a property of UserWrapper, but of UserWrapper.User
The key argument of the sort descriptor can also by a key path, in your case:
[[NSSortDescriptor alloc] initWithKey:#"User.UserName"
ascending:YES
selector:#selector(localizedCaseInsensitiveCompare:)] ;
Although the API and documentation of NSSortDescriptor are a bit inconsistent, the "key" parameter is actually a key path. So, you can just specify #"User.UserName" as the key and your code should work.
I have two entities as follows
Item <----->> Categories
Each item may have multiple categories.
For e.g. There are 5 items each have a sort category as release_Date. I want to sort all these 5 items based on the release_Date with the table view header as the release_Date value of each item.
I want to sort these items based on the category value selected by the user from the popover. Also I want to display the sorted objects in a sectioned table view with the table header being the category values selected.
I am using nsfetchedresultscontroller for this. I am able to sort the items based on the name of the items. But I am finding no luck in sorting them using the relationship attribute.
Any kind of help is appreciable.
if i understand your model correctly you can do this with multiple sort descriptors pretty easily.
NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:#"name" ascending:NO];
NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:#"Categories.release_Date" ascending:NO];
NSArray *sortDescriptors = #[sortDescriptor1, sortDescriptor2];
[fetch setSortDescriptors:sortDescriptors];
I've got an array which should be sorted by name, but only the half of it gets sorted. I've got a list of entries which can be devided in two parts, the first part comes from a dictionary and only has the property "name", the second part comes from a core data database and got as well the property "addedByUser". both of the lists are inside the tempArray and get added into the _resultsarray, which then directly leads to the cellForRowAtIndexPath. But before, I try to sort _resultsarray by name. Now the problem occurs: first the list without the addedByUser attribute appears (sorted by name) and then the other list (with addedByUser attribute") appears, sorted by name as well. I can't get them to be mixed.
[_resultsarray addObjectsFromArray:tempArray];
// Sort the list
NSSortDescriptor *sort2 = [NSSortDescriptor sortDescriptorWithKey:#"name" ascending:YES];
[_resultsarray sortUsingDescriptors:[NSArray arrayWithObject:sort2]];
What am I doing wrong? Thank you!
Update: I'm very sorry, I forgot to write that both tempArray and _resultsarray are mutable arrays.
Update 2: It seems like it makes a difference if the names start with an uppercase or lowercase character. My updated question is then, is there a way to sort an NSMutableArray no matter if the words start with uppercase or lowercase character?
Update 3: I found out:
NSSortDescriptor *sortByName = [[NSSortDescriptor alloc] initWithKey:#"name" ascending:YES selector:#selector(caseInsensitiveCompare:)];
You should write an NSComparoator like:
_resultsarray = [tempArray sortedArrayUsingComparator:^(id firstObject, id secondObject) {
NSString *name1 = ... ; // firstObject.name or firstObject.addedByUser
NSString *name2 = ... ; // secondObject.name or secondObject.addedByUser
return [name1 compare:name2];
}];
Try this:
NSSortDescriptor *sort2 = [NSSortDescriptor sortDescriptorWithKey:#"name" ascending:YES];
_resultsarray = [tempArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort2]];