2011-07-02 13 views
1

私は運とテーブルビューの配列をソートしようとしているが、これは私がそれをやろうとしている方法です:配列のNSMutableArrayをソートする方法は?

- (void) compare 
    { 

     NSMutableArray *temp = [[NSMutableArray alloc] initWithObjects: nil]; 

     [temp addObjectsFromArray:[self displayedObjects]]; 


     int a = [[self displayedObjects] count]; 

     for (int i = 0; i < (a - 1); i++){ 

      for (int j = 0; j <= (i^2); j++) { 

       NSString *temp1 = [temp objectAtIndex:i]; 

       NSString *temp2 = [temp objectAtIndex:i+1]; 

       if (temp1 > temp2) { 

        [movieIndex replaceObjectAtIndex:i withObject:temp2]; 

        [movieIndex replaceObjectAtIndex:i+1 withObject:temp1]; 

       } 
      } 
     } 

     [self movieIndex]; 

     [[self tableView] reloadData]; 

    } 

displayedObjectsは、ユーザーが新しいオブジェクトを追加することができ、配列の配列です。このオブジェクトは次の形式のムービーインスタンスです。

テーブルビューセルにはムービーのタイトルがあります。そのタイトルの "in"には、映画に関する詳細があるテーブルビューがあります。私がしたいのは、表示された映画のタイトルを昇順に並べ替えることですが、私のコードは機能していないようです。


displayedObjectsは、3つのNSStringと1つのNSNumberのセットです。これは、オブジェクトは以下のクラスを形成し得る:

#import <Foundation/Foundation.h> 

@interface Movie : NSObject 
    { 
     NSString *_title; 
     NSString *_gender; 
     NSString *_location; 
     NSNumber *_publicationYear; 
    } 

@property (nonatomic, retain) NSString *location; 
@property (nonatomic, retain) NSString *title; 
@property (nonatomic, retain) NSString *gender; 
@property (nonatomic, retain) NSNumber *publicationYear; 

+ (id)movieWithTitle:(NSString *)title 
       gender:(NSString *)gender 
        year:(NSUInteger)year 
       location:(NSString *)location; 

- (id)initWithTitle:(NSString *)title 
       gender:(NSString *)gender 
        year:(NSUInteger)year 
       location:(NSString *)location; 

@end 

答えて

0

私はすでに提案し、同じことにブロックの代替を示唆してよ、誰もまだそれを示唆していない、と私は個人的にブロックを愛しているので:

[myArray sortUsingComparator:^NSComparisonResult(id obj1, id obj2) { 
    ... 
}]; 
+0

[movieIndex sortUsingComparator:私はそれを使用して働かせた^ NSComparisonResult(IDのOBJ1、IDのOBJ2){ NSStringの* name1 = obj1; NSString * name2 = obj2; 戻り値[name1 localizedCaseInsensitiveCompare:name2]; } ];しかし、私はsigbartエラーを取得する "キャッチされていない例外 'NSInvalidArgumentException'の理由でアプリを終了する理由: ' - [ムービーlocalizedCaseInsensitiveCompare:]:インスタンス0x5912da0に送信されたセレクタが認識されません" – Morpheas1983

+0

私の提案とは関係ありません...あなたの "..."の実装だけ。 – Steve

0

を私はまた、あなたのアプローチを試してみました

NSSortDescriptor *titleSorter= [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES]; 

[movieIndex sortUsingDescriptors:[NSArray arrayWithObject:titleSorter]];