0

私は以下のような3つの配列を持っています

names                      birthdate                remanning 

"Abhi Shah",                 "01/14",                  300
"Akash Parikh",              "12/09/1989",             264
"Anand Kapadiya",            "12/01",                  256
"Annabella Faith Perez",     "03/02",                  347
"Aysu Can",                  "04/14/1992",             25
"Chirag Pandya"              "10/07/1987"              201

plz NSDictionaryにこの3つの配列を追加する方法と、「残りの」配列に従って辞書全体を順序付け(昇順)した後のコードを教えていただければ、非常に役立ちます。

Dicでは、すべてが変更される必要があることに注意してください。残りのアレイだけではありません。名前と生年月日は、残りの日数が変更されるのと同じ方法で変更する必要があります

よろしくお願いします

4

3 に答える 3

3

プロジェクト のデザインを変更し、プロパティを持つのと同じモデルを作成することをお勧めします。

@interface YourModel : NSObject
    @property (strong) NSString *name;
    @property (strong) NDDate *birthDate;
    @property NSInteger remaining;
@end

次に、クラスにNSMutableArrayを作成し、それらを追加します。

これにより、3つの並列配列を処理するよりも、検索、並べ替え、フィルタリングなどの作業が簡単になります。

于 2013-03-20T07:19:34.327 に答える
1

Anoopによって提案された設計を使用する場合、ブロックを使用したソートコードは次のようになります。

NSArray *sortedArray = [yourArray sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
    NSInteger first = [(YourModel*)a remaining];
    NSInteger second = [(YourModel*)b remaining];
    return [first compare:second];
}];
于 2013-03-20T07:30:29.823 に答える
1

辞書または任意の構造の各レコード(名前、生年月日、リマン)を取得する必要があります。そして、その辞書の配列を作成する必要があります。要件に従って配列をソートするには、任意のソートメカニズムを使用できます。

-(void)sort
{
    //This is the array of dictionaries, where each dictionary holds a record
    NSMutableArray * array; 
    //allocate the memory to the mutable array and add the records to the arrat

    // I have used simple bubble sort you can use any other algorithm that suites you
    //bubble sort
    //
    for(int i = 0; i < [array count]; i++)
    {
        for(int j = i+1; j < [array count]; j++)
        {
            NSDictionary *recordOne = [array objectAtIndex:i];
            NSDictionary *recordTwo = [array objectAtIndex:j];

            if([[recordOne valueForKey:@"remaining"] integerValue] > [[recordTwo valueForKey:@"remaining"] integerValue])
            {
                [array xchangeObjectAtIndex:i withObjectAtIndex:j];
            }
        }   
    }

    //Here you get the sorted array
}

お役に立てれば。

于 2013-03-20T07:31:51.163 に答える