Trying an ObjC sort, can't get terminology correct

G’day

After Rics example of some ObjC code in another thread, i though I’d play around and try and write a simple sort.

Can’t get it right tho.

Any guidelines please?

I get ‘Unrecognized selector sent to class’ error

Regards

Santa


#import "ObjCFile.h"


@implementation ObjCFile
 +(id)organizeStuffFrom:(NSMutableArray *)theClientListArray{
	NSArray *theClientList2 = [[NSArray alloc]init];
	theClientList2 = [theClientListArray  sortedArrayUsingSelector:@selector(Compare:)];
	
 	return theClientList2;
}
@end

Hi,

ObjC methods are case sensitive and start generally with a lowercase letter.
Actually there is no need to create a new array, the method will return a new array object


 +(id)organizeStuffFrom:(NSMutableArray *)theClientListArray{
		return [theClientListArray  sortedArrayUsingSelector:@selector(compare:)];
}

Note: consider that the result array is not mutable

G’day Stefan, & thanks.

However, that build fails with three errors.

'Methid definition is not in @implementation context

my .h file is…


#import <Cocoa/Cocoa.h>

@interface ObjCFile : NSObject {
	
}

+(id)organizeStuffFrom:(NSMutableArray *)theClientListArray;
@end

Also, how do I end up with a mutable array out of this?

Regards

Santa

it should compile error free with you syntax.
As the return class is specified, I recommend to return an explicit NSMutableArray


+ (NSMutableArray *)organizeStuffFrom:(NSMutableArray *)theClientListArray{
	return [NSMutableArray arrayWithArray:[theClientListArray sortedArrayUsingSelector:@selector(compare:)]];
}


Darn, it still won’t compile; same 3 errors.

Regards

Santa

Ok, I left out the @implementation etc bit, but it compiles, and I still get

I get ‘Unrecognized selector sent to class’ error

Regards

Santa

it looks like you method call (in the ASOC environment) is misspelled

Hi Santa,

Did you fix the compare: method’s spelling as it was suggested by Stefan? Like he said, ObjC is case-sensitive, so Compare: won’t work, compare: will. Same rule applies for ObjC methods called from ASOC. :slight_smile:

Yeah, stupid mistake by stupid learner

The original calling syntax was organizeStuffFrom_in_

and of course it expected two inputs didn’t it.

Thanks Stefan, it sorts a 48,600 item list in 8 seconds, so I’m very happy!

Regards

Santa

G’day again

I decided to try and write a sort on a two property array 'GraphCount:number GraphClient:string.

I’m completely bewildered by the syntax needed.

So far I’ve got…



#import "ObjCFile.h"

@implementation ObjCFile

+ (NSMutableArray *) organizeStuffFrom:(NSMutableArray *)theClientListArray{
	return [NSMutableArray arrayWithArray:[theClientListArray sortedArrayUsingSelector:@selector(compare:)]];
}


@end

 
+ (NSMutableArray *) organizeNumericallyFrom:(NSMutableArray *)theClientDataStore{
	 NSSortDescriptor : SortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"GraphCount" ascending:YES selector:@selector(compare:)];
 return [NSMutableArray arrayWithArray:[theClientDataStore sortUsingDescriptors:[NSArray arrayWithObjects:SortDescriptor, nil]];
	
}
	 

@end

 

Which has so many errors it’s not funny.

I need to sort the array by property ‘GraphCount:number’ which is a list of integers.

Guidance please?

Regards

Santa

Hi Santa,

there are a lot of mistakes.
First of all, not a mistake but a naming convention violation, use always variable names beginning with a lowercase letter.

¢ In the return line a bracket is missing at the end (must be 3 brackets).
¢ the correct initializer syntax is

 NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"GraphCount" ascending:YES selector:@selector(compare:)];

no colon, but an asterisk as a pointer identifier

Unfortunately your custom method will never work in this case, because sortUsingDescriptors: expects key/value compliant objects in the array, and the method sorts the receiver itself, there is no return value.
For example, assuming the array contains NSDictionary objects with a name and age key, you can sort the array by name or by age using an appropriate NSSortDescriptor.

To sort an array of NSNumber objects numerically, either write your own comparison method for @selector(myMethod:) which must result a NSComparisonResult value,
or - since 10.6 - MSMutableArray has a new sortWithOptions:usingComparator: method which sorts the array by a given NSComparator block