iphone - Sorting an array against another array with substitution -


i have array of days (in string format) program receives user input somewhere between 1-7 in length. array received has day name in full text.

an example of array might receive is: ["tuesday", "monday", "thursday"]

what i'm trying sort array monday sunday , convert full text names abbreviations. sort function above array ideally return: ["m", "tu", "th"].

duplicates of same day never appear, there never less 1 item , never more 7.

thanks.

it's crude, rough ui user selecting days from:

enter image description here

i used selected answer adapted add 1 place needed it. adapted follows:

-(nsarray*)array:(nsarray*)array collect:(id(^)(id object))block {     nsmutablearray * result = [ nsmutablearray array ] ;     for( id object in array ) { [ result addobject:block( object) ] ; }     return result ; }  -(nsarray*)arraybysortingandabbreviatingdaynames:(nsarray*)arraytosort {     nsarray * daynames = @[ @"monday", @"tuesday", @"wednesday", @"thursday", @"friday",     @"saturday", @"sunday" ] ;     nsarray * abbreviations = @[ @"m", @"tu", @"w", @"th", @"f", @"sa", @"su" ] ;     nsarray * array = [ self array:arraytosort collect:^(nsstring * dayname){         return @([ daynames indexofobject:dayname ]) ;     } ] ;     array = [ array sortedarrayusingselector:@selector( compare: ) ] ;     array = [ self array:array collect:^(nsnumber * index){         return abbreviations[ [ index integervalue ] ] ;     }];     return array ; } 

@implementation nsarray (daynamething)  -(nsarray*)collect:(id(^)(id object))block {     nsmutablearray * result = [ nsmutablearray array ] ;     for( id object in self ) { [ result addobject:block( object) ] ; }     return result ; }  -(nsarray*)arraybysortingandabbreviatingdaynames {     nsarray * daynames = @[ @"monday", @"tuesday", @"wednesday", @"thursday", @"friday", @"saturday", @"sunday" ] ;     nsarray * abbreviations = @[ @"m", @"tu", @"w", @"th", @"f", @"sa", @"su" ] ;     nsarray * array = [ self collect:^(nsstring * dayname){         return @([ daynames indexofobject:dayname ]) ;     } ] ;     array = [ array sortedarrayusingselector:@selector( compare: ) ] ;     array = [ array collect:^(nsnumber * index){         return abbreviations[ [ index integervalue ] ] ;     }];     return array ; }  @end 

so... if array has user's selected day names, can result want newarray = [ array arraybysortingandabbreviatingdaynames ]..


edit thought of better/simpler way:

nsarray * convertarray(nsarray * input) {     nsarray * daynames = @[ @"monday", @"tuesday", @"wednesday", @"thursday", @"friday", @"saturday", @"sunday" ] ;     nsarray * shortdaynames = @[ @"m", @"tu", @"w", @"th", @"f", @"sa", @"su" ] ;     nsmutablearray * output = [ nsmutablearray array ] ;     for( int index=0; index < 7; ++index )     {         if ( [ input containsobject:daynames[ index ]] )         {             [ output addobject:shortdaynames[ index ]] ;         }     }     return output ;  } 

edit better yet.

nsarray * convertarray(nsarray * input) {     nsarray * daynames = @[ @"monday", @"tuesday", @"wednesday", @"thursday", @"friday", @"saturday", @"sunday" ] ;     nsarray * shortdaynames = @[ @"m", @"tu", @"w", @"th", @"f", @"sa", @"su" ] ;      return [ shortdaynames objectsatindexes:[ daynames indexesofobjectspassingtest:^bool(id obj, nsuinteger idx, bool *stop) {         return [ input containsobject:obj ] ;     }]] ; } 

...ok, think i'm done. :)


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -