objective c - Using NSNumber to sort numerous integers -


ive been doing alot of research , can't quite grasp resolving issue. in application have several text fields save integer values in form of int variables (at least 15 int variables). main goal sort out high low numbers saved text fields.

now use following code convert each seperate integer variable new nsnumber, use sort function sort out new nsnumbers high low?

nsnumber *newnumber = [nsnumber numberwithint: my_int_variable]; 

i feel if redundant way of sorting 12 integer variables , there easier way. thank help

going individual text fields strings ints nsnumbers array sorted array long, clunky trip. worthwhile trip nevertheless.

// assume these uitextfield *field0; uitextfield *field1;     uitextfield *field2;  // make array input , result nsarray *textfields = @[field0, field1, field2]; nsmutablearray *numbers = [@[] mutablecopy];  // prepare 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  (uitextfield *textfield in textfields) {     nsstring *text = textfield.text;     nsnumber *number = [formatter numberfromstring:text];     [numbers addobject:number]; }  // sort...a few choices here, too. taking simplest: [numbers sortusingselector:@selector(compare:)]; nslog(@"ta da: %@", numbers); 

or did want sort text fields based on 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 contents: %@", textfields); 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -