0

Ive been doing alot of research and can't quite grasp resolving this issue. In my application I have several text fields that save integer values in the form of int variables (at least 15 int variables). My main goal is to sort out high to low numbers that were saved from the text fields.

Now would I use the following code to convert each seperate integer variable into a new NSNumber, than use a sort function to sort out the new NSNumbers from high to low?

NSNumber *newNumber = [NSNumber numberWithInt: my_int_variable];

I just feel as if this is a redundant way of sorting 12 integer variables and there is a easier way. Thank you for the help

rmaddy
  • 314,917
  • 42
  • 532
  • 579
pLokie
  • 47
  • 6
  • You can use the "C" library functin `qsort()`, see this [SO Answer](http://stackoverflow.com/a/1788048/451475). But they will need to be in a "C" array. So if you need to put them in an array you might as well use an `NSArray` of `NSNumbers`. – zaph May 11 '15 at 22:22
  • Don't use separate int variables. Use an array (NSArray) – rmaddy May 11 '15 at 22:27
  • Don't do any work, just put all text fields into an array and sort them as you want. ( use text or integer value from string ) – gaussblurinc May 11 '15 at 22:38
  • 15 isn't really "numerous" in the sense that it would be worth worrying about the overhead of `NSNumber` wrappers. – JWWalker May 11 '15 at 23:14

1 Answers1

1

Going from individual text fields to strings to ints to NSNumbers to an array to a sorted array is long, clunky trip. But a worthwhile trip nevertheless.

// assume these
UITextField *field0;
UITextField *field1;    
UITextField *field2;

// make an array for the input and the result
NSArray *textFields = @[field0, field1, field2];
NSMutableArray *numbers = [@[] mutableCopy];

// prepare a number formatter
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterDecimalStyle;
// lots of choices here.  see https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSNumberFormatter_Class/index.html

for (UITextField *textField in textFields) {
    NSString *text = textField.text;
    NSNumber *number = [formatter numberFromString:text];
    [numbers addObject:number];
}

// sort...a few choices here, too. taking the simplest:
[numbers sortUsingSelector:@selector(compare:)];
NSLog(@"ta da: %@", numbers);

Or did you want to sort the text fields based on their contents? Doable too:

NSMutableArray *textFields = [@[field0, field1, field2] mutableCopy];

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterDecimalStyle;

[textFields sortedUsingComparator: ^(id objA, id objB) {
     NSString *textA = ((UITextField *)objA).text;
     NSString *textB = ((UITextField *)objB).text;

     NSNumber *numberA = [formatter numberFromString:textA];
     NSNumber *numberB = [formatter numberFromString:textB];

     return [numberA compare:numberB];
}];

NSLog(@"text fields in order of their contents: %@", textFields);
danh
  • 62,181
  • 10
  • 95
  • 136