2017-02-14 6 views
0

私は2つのクラス、すなわちViewControllerBViewControllerを持っています。 BViewControllerクラスには、名前、住所、および評価のプロパティがあります。以下はObjective-Cでのカスタムオブジェクトの配列の並べ替え

私は彼らが評価昇順で並べられていること、そのような配列内のオブジェクトを配置して行くにはどうすればよいViewController.m

ためのコードはありますか?さらに、それらをビューでどのように表示するのですか?お使いのアレイ

NSMutableArray *arrMain = [NSMutableArray arrayWithObjects:b, c, d, e ,f, nil]; 

今すぐソート評価

ホープ・テスト済みキー

NSArray *arrResult; 
arrResult = [arrMain sortedArrayUsingComparator:^NSComparisonResult(id a, id b) { 
    NSNumber *first = [(BViewController*)a rating]; 
    NSNumber *second = [(BViewController*)b rating]; 

    NSComparisonResult result = [first compare:second]; 
    return result; 
}]; 

に基づいて、それはあなたをお手伝いします

- (void)viewDidLoad { 

     [super viewDidLoad]; 

    BViewController *b=[[BViewController alloc]init]; 
     [email protected]"x"; 
     [email protected]"bangalore"; 
     [email protected]; 

    BViewController *c=[[BViewController alloc]init]; 
     [email protected]"y"; 
     [email protected]"bangalore"; 
     [email protected]; 

    BViewController *d=[[BViewController alloc]init]; 
     [email protected]"z"; 
     [email protected]"bangalore"; 
     [email protected]; 

    BViewController *e=[[BViewController alloc]init]; 
     [email protected]"abc"; 
     [email protected]"bangalore"; 
     [email protected]; 

    BViewController *f=[[BViewController alloc]init]; 
     [email protected]"xyz"; 
     [email protected]"bangalore"; 
     [email protected]; 
    } 

答えて

1


おかげ

+0

storyBoardのようなstoryBoardのようなものを教えてもらえますか?結果を表示するには、フィールドフィールドやラベルが新しくなっています。 – taj

+0

@taj入力には、UITextfieldsを使用します。並べ替え配列のUIButton結果値を表示するにはUILabelを押します。 –

+0

ねえ、コンソールで出力を見るにはどうすればいいですか?私はNSLog(@ "%@"、arrResult)を置くので。 「」、「」、「」、「」、「」などの出力が表示されないこのように来ています – taj

1

Appleはエレガントな方法であなたが欲しいものを行うためのAPIがあります。

NSMutableArray *yourArray = [NSMutableArray arrayWithObjects:b, c, d, e ,f, nil]; 
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"rating" ascending:YES]; 
[yourArray sortUsingDescriptors:@[sortDescriptor]]; 

あなたの配列は今うまくソートされています。