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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.