I have an NSMutableArray and on every index I have another NSMutableArray but of different length like 2, 3, 4. I want to sort main array that the inside array who have bigger length come on top.
You can sort them using a descriptor:
NSSortDescriptor *sd = [[NSSortDescriptor alloc] initWithKey:#"count" ascending:NO];
[myArray sortUsingDescriptors:#[sd]];
The above code creates one sort descriptor on the property called "count" in descending order.
//Create a sort descriptor based on count using NSSortDescriptor class as-
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"count" ascending:NO];
//& sort your main array using sort descriptor
[mainArray sortUsingDescriptors:#[sortDescriptor]];
Related
I have NSMutableArray named "allConversations" that contains a custom class "Users".
every user have property "lastMessageDate".
e.x : user.lastMessageDate;
what is the best way to sort the array by date?
I have this code :
NSMutableArray *arrCopy = [[NSMutableArray alloc]initWithArray:unSortedArr];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"lastMessageDate" ascending:TRUE];
[arrCopy sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
but I don't know how to implement it in this situation
it seems all you are missing is the assignment. This should work for you.
arrCopy = [arrCopy sortedArrayUsingDescriptors:#[[[NSSortDescriptor alloc] initWithKey:#"createdAt" ascending:YES]]];
I'm using a statement in sqlite to select specific object from table:
NSString *statment = [NSString stringWithFormat:#"SELECT * FROM %# ORDER BY %# DESC LIMIT 1", TRAP_TABLE, DISTANCE_TO_CLOSE_POINT];
I want to do the same thing using core data.
How should I mix the NSPredicate & NSSortDescriptor in order to do the same?
EDIT:
This is what I did, didn't tried it yet:
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:DISTANCE_TO_CLOSE_POINT ascending:NO selector:#selector(localizedCompare:)];
The NSPredicate is like the where clause of a SQL statement. Your example doesn't have a where clause.
In a NSFetchRequest you can add sort descriptors to handle the 'order by'. Also in the NSFetchRequest you can set a fetch limit.
From there you pass the NSFetchRequest to the NSManagedObjectContext and receive your results.
Update 1
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:DISTANCE_TO_CLOSE_POINT ascending:NO selector:#selector(localizedCompare:)];
That is overkill in this situation. I am guessing the value you are sorting on is a number which means you don't need localizedCompare: since you are not working with strings. So you can simplify it to:
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:DISTANCE_TO_CLOSE_POINT ascending:NO];
And if you are in an ARC project, even reduce it down to:
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:DISTANCE_TO_CLOSE_POINT ascending:NO];
Which can then be thrown directly into the NSFetchRequest:
[fetchRequest setSortDescriptors:#[[NSSortDescriptor sortDescriptorWithKey:DISTANCE_TO_CLOSE_POINT ascending:NO]]];
I am constructing a NSFetchedResultsController for my entity, which has two attributes, let's say a shortName and a longName. All of the managed objects only have either the shortName or the longName.
How can I sort the objects based on both attributes (or rather the one which is not null) at the same time?
Clearly the following will not work in my case:
NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:#"shortName" ascending:YES];
NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:#"longName" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1, sortDescriptor2, nil];
It's quite interesting, but Im almost sure it is not possible to do. Just assigning shortName to the longName attribute, if there is no longName, while inserting to your db would be the best solution for me and simply sorting on longName.
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]];
I'm struggling with trying to sort an array of dictionaries.
My dictionaries have a couple of values of interest, price, popularity etc.
Any suggestions?
Use NSSortDescriptor like this..
NSSortDescriptor * descriptor = [[NSSortDescriptor alloc] initWithKey:#"interest" ascending:YES];
stories = [stories sortedArrayUsingDescriptors:#[descriptor]];
recent = [stories copy];
stories is the array you want to sort. recent is another mutable array which has sorted dictionary values. Change the #"interest" with the key value on which you have to sort.
All the best
[array sortUsingDescriptors:[NSArray arrayWithObjects:[NSSortDescriptor sortDescriptorWithKey:#"YOUR-KEY" ascending:YES], nil]];
I don't really want to break it into multiple lines. It's already simple enough for me.
You can just traverse from the parent to the required child property. For e.g.
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:#"parent.child.child" ascending:YES];
Update , sortUsingDescriptors is now sortedArrayUsingDescriptors
So, its like:
items = [items sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]];
Write a sort function that compares the relevant fields in two dictionaries. When you have this version you can for example use NSArray#sortedArrayUsingFunction:context: to sort your array.
See NSArray Class Reference
array = [array sortedArrayUsingDescriptors:[NSArray arrayWithObjects:[NSSortDescriptor sortDescriptorWithKey:#"YOUR-KEY" ascending:YES], nil]];
Remember assign to array