views:

27

answers:

1

hi i have an array of objects which need to be sorted (alphabet on name)

ArtistVO *artist1 = [ArtistVO alloc];
artist1.name = @"Trentemoeller";
artist1.imgPath = @"imgPath";

ArtistVO *artist2 = [ArtistVO alloc];
artist2.name = @"ATrentemoeller";
artist2.imgPath = @"imgPath2";


ArtistVO *artist3 = [ArtistVO alloc];
artist3.name = @"APhextwin";
artist3.imgPath = @"imgPath2";    

//NSLog(@"%@", artist1.name);
NSMutableArray *arr = [NSMutableArray array];
[arr addObject:artist1];
[arr addObject:artist2];
[arr addObject:artist3];


NSSortDescriptor *lastDescriptor =
[[[NSSortDescriptor alloc]
  initWithKey:@"name"
  ascending:YES
  selector:@selector(localizedCaseInsensitiveCompare:)] autorelease];    

NSArray * descriptors =
[NSArray arrayWithObjects:lastDescriptor, nil];
NSArray * sortedArray =
[arr sortedArrayUsingDescriptors:descriptors];    

NSLog(@"\nSorted ...");
NSEnumerator *enumerator;
enumerator = [sortedArray objectEnumerator];

ArtistVO *tmpARt;
while ((tmpARt = [enumerator nextObject])) NSLog(@"%@", tmpARt.name);    

works fine. but now i need to split this awway up for usage in a grouped uitableview

    self.sortedKeys =[[NSArray alloc]
                  initWithObjects:@"{search}",@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",nil];


NSMutableArray *arrTemp0 = [[NSMutableArray alloc]
                     initWithObjects:@"000",nil];    
NSMutableArray *arrTemp1 = [[NSMutableArray alloc]
                     initWithObjects:@"Andrew",@"Aubrey",@"Aalice", @"Andrew",@"Aubrey",@"Alice",@"Andrew",@"Aubrey",@"Alice",nil];
NSMutableArray *arrTemp2 = [[NSMutableArray alloc]
                     initWithObjects:@"Bob",@"Bill",@"Bianca",@"Bob",@"Bill",@"Bianca",nil];
NSMutableArray *arrTemp3 = [[NSMutableArray alloc]
                     initWithObjects:@"Candice",@"Clint",@"Chris",@"Candice",@"Clint",@"Chris",nil];
NSMutableArray *arrTemp4 = [[NSMutableArray alloc]
                     initWithObjects:@"Dandice",@"Dlint",@"Dhris",nil];
NSDictionary *temp =[[NSDictionary alloc]
                     initWithObjectsAndKeys:arrTemp0, @"{search}", arrTemp1,@"A",arrTemp2,
                     @"B",arrTemp3,@"C",arrTemp4,@"D",nil];


self.tableContents =temp;

so all Artist with first letter "a" come in one array ... with "b" in one array and so on.

do i need to do some string comparism or is there a better approach?

A: 

How about:

Create an empty NSMutableDictionary.

Loop through all your strings. For each string:

  • If string is empty, ignore this string.
  • Get a NSString containing first character of the string converted to uppercase. This will be your dictionary key.
  • Look in the dictionary to see if it already contains this key.
  • If not, create a new NSMutableArray containing just your string and add it as the value to this new key.
  • If so, add this string to the end of the existing array.

End of loop.

invariant
thanks. thought there might be a smaller way like: "return array from array by selctor" somthing similar to the sorting descriptors
Alex Milde