I have an array of custom objects. Objects contain status : paid or unpaid.
I want to sort array like :
On first click, sort array to display paid records first and then unpaid records.
On second click, sort array to show unpaid records and then paid records.
Any help ?
you can try below like statement:
NSSortDescriptor *Sorter = [NSSortDescriptor sortDescriptorWithKey:#"yourfield" ascending:YES selector:#selector(caseInsensitiveCompare:)];
[yourArray sortUsingDescriptors:[NSArray arrayWithObject:Sorter]];
You can do as :
- (IBAction)sort:(id)sender {
static BOOL isPaidFirst=YES;
NSSortDescriptor *Sorter = [NSSortDescriptor sortDescriptorWithKey:#"isPaid"
ascending:!isPaidFirst
selector:#selector(compare:)];
[self.array sortUsingDescriptors:[NSArray arrayWithObject:Sorter]];
isPaidFirst=!isPaidFirst;
for (MyObject *obj in self.array) {
NSLog(#"-->%#, %ld, %d",obj.courseName, obj.totalFee, obj.isPaid);
}
NSLog(#"------------------");
}
Here MyObject is as :
#interface MyObject : NSObject
#property(strong)NSString *courseName;
#property NSInteger totalFee;
#property BOOL isPaid;
#end
Related
self.objectSet = [NSMutableArray array];
//add objects to set
[self.objectSet sortUsingComparator:^NSComparisonResult(id a, id b) {
NSDate *firstDate = [(Entity *)a createdAt];
NSDate *secondDate = [(Entity *)b createdAt];
return [secondDate compare:firstDate];
}];
I want to sort the nsmutablearray by createdAt date. However the above code has no effect on the displayed table view. The data is unsorted.
Any ideas whats wrong?
Did you try to sort with sort descriptors?
Is it the same effect?
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:#"createdAt" ascending:YES];
[self.objectSet sortUsingDescriptors:#[sortDescriptor]];
I have an NSArray *userInputs, this array contains custom objects "UserInput".
#interface UserInput :NSObject {
NSString *page_id;
#end
Each userInput ( instance of UserInput) belog to BookPage.
#interface BookPage: NSobject {
NSString *pagePrefix; ( exemple : pagePrefix = #"K1")
#end
From each userInput i know to witch BookPage it's belong like this :
BookPage *bookPage = [self.book pageWithID:userInput.page_id];
how i can sort an array of userInputs ( instances of UserInputs) with the pagePrefix Key. Thanks for your answers ?
You can sort the array of custom object like this:
NSSortDescriptor *aSortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"pagePrefix" ascending:YES];
[userInputs sortUsingDescriptors:[NSArray arrayWithObject:aSortDescriptor]];
You can sort array like(considering your array contains strings) -
sortedArray = [userInput sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
I think there is a book property in the middle?
NSSortDescriptor *aSortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"book.pagePrefix" ascending:YES];
[userInputs sortUsingDescriptors:[NSArray arrayWithObject:aSortDescriptor]];
I have a NSMutableArray of objects, below is the struct:
//object at index i
locations {
NSString* address;
NSString* state;
NSNumber* distance;
}
I have 10000 object like the above structure in the NSMutableArray.
How do order this array so that the locations are in order by the NSNumber distance?
I tried this:
lowToHigh = [NSSortDescriptor sortDescriptorWithKey:#"distance" ascending:YES];
[locationArray sortUsingDescriptors:[NSArray arrayWithObject:lowToHigh]];
is using the sortDescriptorWithKey:#"distance" wrong? since distance isn't actually a key, it NSNumber* distance.
edit in my header #property (strong, nonatomic)NSNumber* _distance; and then #synthesize _distance = distance; in my methods file but this is in locationObjects.* object class. Then I import this in my current class I am doing this sorting. Is that an issue?
How I import is locationObjects* locObj = [[locationObjects alloc] init];
This is what I use:
NSSortDescriptor *lowToHigh = [[NSSortDescriptor alloc] initWithKey:#"distance"
ascending:YES];
NSArray *mySortDescriptors = [NSArray arrayWithObject:lowToHigh];
NSArray *sortedArray = [locationArray sortedArrayUsingDescriptors:mySortDescriptors];
I want to order NSMutableArray contain objects according to two of object property one of them has double type second has NSString ?
I tried to use this method but I got exception :
NSString * LASTNAME = #"lastName";
NSString * GRADE = #"grade";
NSSortDescriptor *lastDescriptor =
[[NSSortDescriptor alloc]
initWithKey:LASTNAME
ascending:YES
selector:#selector(localizedCaseInsensitiveCompare:)];
NSSortDescriptor *firstDescriptor =
[[NSSortDescriptor alloc]
initWithKey:GRADE
ascending:YES
selector:#selector(localizedCaseInsensitiveCompare:)]];
NSArray * descriptors =
[NSArray arrayWithObjects:lastDescriptor, firstDescriptor, nil];
NSArray * sortedArray =
[students sortedArrayUsingDescriptors:descriptors];
#interface Student : NSObject
{
NSString * firstName;
NSString * lastName;
double grade;
}
NSNumber doesn't respond to localizedCaseInsensitiveCompare:. You want just plain compare: for the number.
Numbers don't have a case, try using compare: for GRADE
After Edit: You didn't say what error you got. Did the code you posted even compile? You have a typo in your firstDescriptor -- one too many "]" at the end of the method. You probably fixed that when you took out the compare: (which shouldn't have fixed anything).
Remove localizedCaseInsensitiveCompare
NSSortDescriptor *firstDescriptor =
[[NSSortDescriptor alloc]
initWithKey:GRADE
ascending:YES]];
I'm currently using NSMutableArrays in my developments to store some data taken from an HTTP Servlet.
Everything is fine since now I have to sort what is in my array.
This is what I do :
NSMutableArray *array = [[NSMutableArray arrayWithObjects:nil] retain];
[array addObject:[NSArray arrayWithObjects: "Label 1", 1, nil]];
[array addObject:[NSArray arrayWithObjects: "Label 2", 4, nil]];
[array addObject:[NSArray arrayWithObjects: "Label 3", 2, nil]];
[array addObject:[NSArray arrayWithObjects: "Label 4", 6, nil]];
[array addObject:[NSArray arrayWithObjects: "Label 5", 0, nil]];
First column contain a Label and 2nd one is a score I want the array to be sorted descending.
Is the way I am storing my data a good one ? Is there a better way to do this than using NSMutableArrays in NSMutableArray ?
I'm new to iPhone dev, I've seen some code about sorting but didn't feel good with that.
Thanks in advance for your answers !
This would be much easier if you were to create a custom object (or at least use an NSDictionary) to store the information, instead of using an array.
For example:
//ScoreRecord.h
#interface ScoreRecord : NSObject {
NSString * label;
NSUInteger score;
}
#property (nonatomic, retain) NSString * label;
#property (nonatomic) NSUInteger score;
#end
//ScoreRecord.m
#import "ScoreRecord.h"
#implementation ScoreRecord
#synthesize label, score;
- (void) dealloc {
[label release];
[super dealloc];
}
#end
//elsewhere:
NSMutableArray * scores = [[NSMutableArray alloc] init];
ScoreRecord * first = [[ScoreRecord alloc] init];
[first setLabel:#"Label 1"];
[first setScore:1];
[scores addObject:first];
[first release];
//...etc for the rest of your scores
Once you've populated your scores array, you can now do:
//the "key" is the *name* of the #property as a string. So you can also sort by #"label" if you'd like
NSSortDescriptor * sortByScore = [NSSortDescriptor sortDescriptorWithKey:#"score" ascending:YES];
[scores sortUsingDescriptors:[NSArray arrayWithObject:sortByScore]];
After this, your scores array will be sorted by the score ascending.
You don't need to create a custom class for something so trivial, it's a waste of code. You should use an array of NSDictionary's (dictionary in ObjC = hash in other languages).
Do it like this:
NSMutableArray * array = [NSMutableArray arrayWithObjects:
[NSDictionary dictionaryWithObject:#"1" forKey:#"my_label"],
[NSDictionary dictionaryWithObject:#"2" forKey:#"my_label"],
[NSDictionary dictionaryWithObject:#"3" forKey:#"my_label"],
[NSDictionary dictionaryWithObject:#"4" forKey:#"my_label"],
[NSDictionary dictionaryWithObject:#"5" forKey:#"my_label"],
nil];
NSSortDescriptor * sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:#"my_label" ascending:YES] autorelease];
[array sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];