Luis Ramos
Luis Ramos

Reputation: 529

Not able to have array sort

I have been teaching myself programming for about 4 months now. I use this site and another forum to help work myself through issues and a friend gave me his old book from school. Just so you know why I might miss an obvious mistake.

I am working on a program that has songs and artist listed. I want this to sort them and then display them in ascending order by artist. I feel like I am almost there but am missing something.

The results will display but not display in order. I am thinking I need to add something stating to print in ascending order or list next song, but I am not sure what. Here is my entire code thank you for the help:

SONG INTERFACE

#import <Foundation/Foundation.h>

@interface Song : NSObject

-(void) setTitle:(NSString *) songTitle;
-(void) setArtist:(NSString *) songArtist;
-(NSString *) Title;
-(NSString *) Artist;

@end

SONG IMPLEMENTATION

 #import "Song.h"

@implementation Song

{
    NSString *Title;
    NSString *Artist;
}

-(void) setTitle:(NSString *)songTitle;

{
    Title = [NSString stringWithString: songTitle];
}

-(void) setArtist:(NSString *)songArtist;

{
    Artist = [NSString stringWithString:songArtist];
}

-(NSString *) Title

{
    return Title;
}

-(NSString *) Artist

{
    return Artist;
}

-(NSComparisonResult) compareArtist: (id) element

{
    return [Artist compare: [element Artist]];
}

@end

LIBRARY INTERFACE

#import <Foundation/Foundation.h>
#import "Song.h"

@interface Library : NSObject

@property (nonatomic, strong) NSMutableArray *list;

-(id) initList;

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

-(void) listSongs;

-(void) sort;

@end

LIBRARY IMPLEMENTATION

    #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) sort

{
    [list sortUsingSelector: @selector(Artist:)];
}

-(void) listSongs

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

    for (Song *song in list)
        NSLog(@"%-20s by %s", [song.Title UTF8String], [song.Artist UTF8String]);
}


@end

MAIN

#import <Foundation/Foundation.h>
#import "Song.h"
#import "Library.h"

int main(int argc, const char * argv[])

{

    @autoreleasepool {


         // insert code here...
        Song *sonlis1 = [[Song alloc] init];
        Song *sonlis2 = [[Song alloc] init];
        Song *sonlis3 = [[Song alloc] init];
        Song *sonlis4 = [[Song alloc] init];
        Song *sonlis5 = [[Song alloc] init];
        Song *sonlis6 = [[Song alloc] init];
        Song *sonlis7 = [[Song alloc] init];
        Song *sonlis8 = [[Song alloc] init];
        Song *sonlis9 = [[Song alloc] init];
         Song *sonlis10 = [[Song alloc] init];

        Library *myList = [[Library alloc] initList];

        //song 1
        [sonlis1 setTitle: @"Holiday"];
        [sonlis1 setArtist: @"Madonna"];

        //song 2
        [sonlis2 setTitle: @"One"];
        [sonlis2 setArtist: @"Metalica"];


        //song  3
        [sonlis3 setTitle: @"girlfriend"];
        [sonlis3 setArtist: @"Nsync"];


        //song 4
        [sonlis4 setTitle: @"Tush"];
        [sonlis4 setArtist: @"ZZtop"];

        //song5
        [sonlis5 setTitle: @"Jump"];
        [sonlis5 setArtist: @"Van Halen"];

        //song 6
        [sonlis6 setTitle: @"Aeroplane"];
        [sonlis6 setArtist: @"Red Hot Chilli Peppers"];

         //song 7
        [sonlis7 setTitle: @"Stand"];
        [sonlis7 setArtist: @"REM"];

         //song 8
        [sonlis8 setTitle: @"Betterman"];
        [sonlis8 setArtist: @"Pearl Jam"];

        //song 9
        [sonlis9 setTitle: @"Try"];
        [sonlis9 setArtist: @"Pink"];

        //song 10
        [sonlis10 setTitle: @"Madness"];
        [sonlis10 setArtist: @"Muse"];

        [myList addSong:sonlis1];
        [myList addSong:sonlis2];
        [myList addSong:sonlis3];
        [myList addSong:sonlis4];
        [myList addSong:sonlis5];
        [myList addSong:sonlis6];
        [myList addSong:sonlis7];
        [myList addSong:sonlis8];
        [myList addSong:sonlis9];
        [myList addSong:sonlis10];

        [myList listSongs];

    }
    return 0;
}

OUTPUT

2013-10-13 14:00:36.202 Final Project[1607:303] The list of my songs
2013-10-13 14:00:36.204 Final Project[1607:303] Holiday              by Madonna
2013-10-13 14:00:36.204 Final Project[1607:303] One                  by Metalica
2013-10-13 14:00:36.205 Final Project[1607:303] girlfriend           by Nsync
2013-10-13 14:00:36.205 Final Project[1607:303] Tush                 by ZZtop
2013-10-13 14:00:36.206 Final Project[1607:303] Jump                 by Van Halen
2013-10-13 14:00:36.206 Final Project[1607:303] Aeroplane            by Red Hot Chilli Peppers
2013-10-13 14:00:36.207 Final Project[1607:303] Stand                by REM
2013-10-13 14:00:36.207 Final Project[1607:303] Betterman            by Pearl Jam
2013-10-13 14:00:36.208 Final Project[1607:303] Try                  by Pink
2013-10-13 14:00:36.208 Final Project[1607:303] Madness              by Muse

Upvotes: 0

Views: 66

Answers (2)

sam-w
sam-w

Reputation: 7687

There are a number of problems here:

  1. You never call [Library sort].
  2. The selector passed to [NSMutableArray sortUsingSelector] should return an NSComparisonResult - you're using [song Artist], which returns an NSString. It looks like you might actually want to use [Song compareArtist], which does return an NSComparisonResult.

Upvotes: 0

Tricertops
Tricertops

Reputation: 8512

Call [myList sort] right before [myList listSongs].

Upvotes: 1

Related Questions