0

I am working on getting a list of songs to sort by artist. I have my code working and can print the songs and artists. Now I want to make it sort and print the list by artist. This is my first attempt at sorting and I know I have to be missing something simple here. I get -- Expected ')'-- and -- use of undeclared identifier 'listSongs' -- This is all in my @implementation file.

Any help is appreciated thank you in advance.

Here is my interface section

@interface Library : NSObject

@property (nonatomic, strong) NSMutableArray *list;

-(id) initList;

-(void) addSong: (Song *) song;

-(void) listSongs;

@end

This is my implementation section

#import "Library.h"

@implementation Library

@synthesize list;

-(id) initList
{
    self = [super init];
    if (self){
        list = [NSMutableArray array];
    }
    return self;
 }

-(id) init
{
    return [self initList];
}


-(void) addSong:(Song *)song

{
    [list addObject: song];
}

-(void) listSongs

{
    NSLog(@"The list of my songs");

    NSSortDescriptor *sorter;

    sorter = [[[NSSortDescriptor alloc] initWithKey:@"artist" ascending: YES]

          NSArray *sortDescriptors = [NSArray arrayWithObjects:sorter]; **// ** Expected ')'**

          [listSongs sortUsingDescriptors:sortDescriptors]; //**use of undeclared identifier 'listSongs'

    for (Song *song in list)
        NSLog(@"%-20s by %s", [song.Title UTF8String], [song.Artist UTF8String]);
}
4
  • Where are you declaring listSongs? Commented Oct 12, 2013 at 18:59
  • I thought I was declaring it right above. I am assuming by your a=question that I am not so i need to go look into that now. If you cant tell I am very new to this and still trying to figure out the terms while working. Thank you! Commented Oct 12, 2013 at 19:04
  • I had it declared in the .h file. I added it to the post now. Commented Oct 12, 2013 at 19:18
  • You have declared a method. Not an NSArray. Commented Oct 12, 2013 at 19:19

2 Answers 2

1

You should use NSArray *sortedArray = [listSongs arraySortedUsingDescriptors:sortDescriptors];

Basically this method returns a sorted array, it is called on an unsorted array and is given an array of sort descriptors.

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

2 Comments

Okay so I need to set an separate array to "hold" the sorted data. I get it. Thank you very much.
If this answer solved your problem you should accept it so people know which answer you went with
0

As I understand from your code, what you're trying to do is to sort list which is declared property.

You need to replace this line

[listSongs sortUsingDescriptors:sortDescriptors];

With

self.list = [self.list sortUsingDescriptors:sortDescriptors];

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.