-3

i got a table view controller which imports a NSObject with NSStrings. in the table view controller where i create the list, is not in alphabetical order. here is the pieces of code i am using:

Songs.h:

#import <Foundation/Foundation.h>

@interface Songs : NSObject
@property (nonatomic, retain) NSString* name;
@property (nonatomic, retain) NSString* description;
@property (assign) int serial;

-(id) initWithName:(NSString*) SongName andDescription:(NSString*)theDescription;

@end

songs.m:

#import "Songs.h"

@implementation Songs
@synthesize name;
@synthesize description;
@synthesize serial;

-(id) initWithName:(NSString *)theName andDescription:(NSString *)theDescription
{
    self = [super init];
    if(self)
    {
        self.name = theName;
        self.description = theDescription;
    }
    return self;
}
@end

and the tableview:

#import "TableViewController.h"
#import "DetailViewController.h"
#import "Songs.h"

@implementation TableViewController

@synthesize allTableData;
@synthesize filteredTableData;
@synthesize letters;
@synthesize searchBar;
@synthesize isFiltered;

NSArray *SongsIndexTitles;
    SongsIndexTitles = @[@"A", @"B", @"C",@"Ç", @"D", @"E", @"F", @"G", @"H", @"I",@"İ", @"J", @"K", @"L", @"M", @"N", @"O", @"Ö", @"P", @"R", @"S",@"Ş", @"T", @"U",@"Ü", @"V", @"Y", @"Z"];

    allTableData = [[NSArray alloc] initWithObjects:
                    [[Songs alloc] initWithName:@"Ağlarsa Anam Ağlar" andDescription:@"Seslendiren: Abidin"],
                    [[Songs alloc] initWithName:@"Aynalı Kemer" andDescription:@"Seslendiren: Abidin"],
                    [[Songs alloc] initWithName:@"Boşuna " andDescription:@"Seslendiren: Abidin"],
                    [[Songs alloc] initWithName:@"Çöpçüler " andDescription:@"Seslendiren: Abidin"],
                    [[Songs alloc] initWithName:@"Ahu Gozlum" andDescription:@"Seslendiren: Abidin"],
                    [[Songs alloc] initWithName:@"Hepsi Senin mi?" andDescription:@"Seslendiren: Abidin"],
                    [[Songs alloc] initWithName:@"Neredesin Sen?" andDescription:@"Seslendiren: Abidin"],
                    [[Songs alloc] initWithName:@"Pamuk" andDescription:@"Seslendiren: Abidin"],
                    [[Songs alloc] initWithName:@"Sen Yoluna Ben Yoluma" andDescription:@"Seslendiren: Abidin "],
                    [[Songs alloc] initWithName:@"Yolcu Yolunda Gerek" andDescription:@"Seslendiren: Abidin "],

and the list goes on and on. the sectioning works great but i could not figure out how to make the song names in each section in alphabetical order; like in A section it should be 1-Ahu Gozlum 2-Aglarsa Anam Aglar 3- Aynali Kemer....

any help would be appreciated.

Christian Schnorr
  • 10,489
  • 8
  • 45
  • 81

2 Answers2

2

Use NSSortDescriptor. Ensure allTableData is an NSMutableArray:

NSSortDescriptor *desc = [NSSortDescriptor sortDescriptorWithKey:@"name"
                                                       ascending:YES];
[allTableData sortUsingDescriptors:@[ desc ]];

Then reload the tableview.

Droppy
  • 9,470
  • 1
  • 18
  • 27
0

This may help you:

NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)];

    NSArray* sortedArray=[allTableData sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];
Bhanupriya
  • 1,164
  • 1
  • 8
  • 18