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]]];
Related
I have an array of dictionaries where dictionary is like this.
Rows = (
"<DriverRowRecord: 0x7f8de3a240d0>",
"<DriverRowRecord: 0x7f8de3a18790>"
);
Sections = "<DriverSectionRecord: 0x7f8de3a2c5a0>";
Here DriverRowRecord and DriverSectionRecord are separate classes. In DriverSectionRecord I have a date property.
#interface DriverSectionRecord : NSObject
#property(nonatomic,retain)NSDate *date;
#end
Now I want to sort the array based on DriverSectionRecord's date property. If the dictionary contains the date key I sort it like this.
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:#"date" ascending:YES];
NSArray *descriptors = [NSArray arrayWithObject:descriptor];
NSArray *Ascendingorder = [Array sortedArrayUsingDescriptors:descriptors];
However, it doesn't work as date is a property of DriverSectionRecord. How can I achieve this?
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:#"Sections.date" ascending:YES];
NSArray *descriptors = [NSArray arrayWithObject:descriptor];
NSArray *Ascendingorder = [Array sortedArrayUsingDescriptors:descriptors];
I got he solution from above comments. Thanks to all for such a great clarification.
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]];
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.
how can i sort my list alphabetically with NSFetchedResultsController, when the text contains HTML tags.
For example, i have to sort this:
- <b>Hello</b>
- Adam
The Result should be:
- Adam
- <b>Hello</b>
This is how i sort it at the moment:
sortString = [[NSString alloc] initWithString:#"title"];
NSSortDescriptor *sortDescriptor2 = [[[NSSortDescriptor alloc] initWithKey:sortString ascending:sortAsc selector:#selector(localizedCaseInsensitiveCompare:)] autorelease];
NSArray *sortDescriptors = [[[NSArray alloc] initWithObjects:sortDescriptor, sortDescriptor2, nil] autorelease];
[fetchRequest setSortDescriptors:sortDescriptors];
So, is there a way to sort "splitted"?
Yes, there is a way: construct a specialized NSSortDescriptor with your own selector. Since NSString has standard method to do what you want, you will need to create a category on NSString named something like "compareWithHTML:", which strips the HTML and does the comparison using "compare:" or "localizedCompare:.
I have a array of objects and need to sort it by rating and after by number of votes descendent(so that if two 5 stars rated elements compare, the one with the most votes should be first)
Is it possible to sort a NSArray by two descriptors:first after rating and then after votes count?
I've found on http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSSortDescriptor_Class/Reference/Reference.html
something like sortUsingDescriptors: but i can't find it anywhere in the docs, think it's deprecated.
Yes you can:
NSSortDescriptor *sortRating = [[NSSortDescriptor alloc] initWithKey:#"rating" ascending:NO];
NSSortDescriptor *sortVotes = [[NSSortDescriptor alloc] initWithKey:#"votes" ascending:NO];
NSArray *sortedArray = [orignalAray sortedArrayUsingDescriptors:[NSArray arrayWithObjects:sortRating, sortVotes, nil]];
[sortRating release], sortRating = nil;
[sortVotes release], sortVotes = nil;
You are basically right. On NSArray there is the
- (NSArray *)sortedArrayUsingDescriptors:(NSArray *)sortDescriptors
method. This will return a new array, sorted according to various descriptors.
- (void)sortUsingDescriptors:(NSArray *)sortDescriptors
does exist, but is on NSMutableArray, which might explain why you couldn't find it in the documentation for NSArray. It accomplishes the same goal, but sorts the array you call it on, rather than returning a new array. Neither is deprecated.
Here's a one-liner.
NSArray *sortedArray = [unsortedArray sortUsingDescriptors:[NSArray arrayWithObjects:[NSSortDescriptor sortDescriptorWithKey#"rating" ascending:NO], [NSSortDescriptor sortDescriptorWithKey#"date" ascending:NO], nil]];
Use - (NSArray *)sortedArrayUsingDescriptors:(NSArray *)sortDescriptors. Here is the documentation: NSArray Class Reference
E.g:
NSSortDescriptor *sortRating = nil;
NSSortDescriptor *sortDate = nil;
NSSortDescriptor *sortRating = [[NSSortDescriptor alloc] initWithKey:#"rating" ascending:NO];
NSSortDescriptor *sortDate = [[NSSortDescriptor alloc] initWithKey:#"date" ascending:NO];
NSArray *sortedArray = [auxArray sortedArrayUsingDescriptors:[NSArray arrayWithObjects:sortRating,sortDate,nil]];
[sortRating release];
sortRating = nil;
[sortDate release];
sortDate = nil;
Cheers!
If you have one NSArray use this one:
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"x_date" ascending:TRUE];
NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:#"x_time" ascending:TRUE];
[tempArray sortUsingDescriptors:[NSArray arrayWithObjects:sortDescriptor,sortDescriptor1, nil]];