次のようなNSArray
日付文字列(つまりNSString)を含むがあります: "Thu、21 May 09 19:10:09 -0700"
日付で並べ替える必要がありNSArray
ます。最初に日付文字列をオブジェクトに変換することを考えましたNSDate
が、オブジェクトで並べ替える方法に行き詰まりましたNSDate
。
ありがとう。
次のようなNSArray
日付文字列(つまりNSString)を含むがあります: "Thu、21 May 09 19:10:09 -0700"
日付で並べ替える必要がありNSArray
ます。最初に日付文字列をオブジェクトに変換することを考えましたNSDate
が、オブジェクトで並べ替える方法に行き詰まりましたNSDate
。
ありがとう。
NSMutableArray
タイプが「beginDate」のフィールドを持つオブジェクトがあれば、以下のようNSDate
に使用NSSortDescriptor
します。
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"beginDate" ascending:TRUE];
[myMutableArray sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[sortDescriptor release];
日付をNSDate
オブジェクトとしてNS(Mutable)Arrayに格納し、-[NSArray sortedArrayUsingSelector:
または-[NSMutableArray sortUsingSelector:]
をパラメーターとして使用して渡し@selector(compare:)
ます。この-[NSDate compare:]
メソッドは、日付を昇順で並べ替えます。これは、を作成するよりもNSSortDescriptor
簡単で、独自の比較関数を作成するよりもはるかに簡単です。(NSDate
オブジェクトは、少なくともカスタムコードで達成できるのと同じくらい効率的に相互に比較する方法を知っています。)
次のようなものを使用することもできます。
//Sort the array of items by date
[self.items sortUsingComparator:^NSComparisonResult(id obj1, id obj2){
return [obj2.date compare:obj1.date];
}];
ただし、これは日付がNSDate
むしろNString
として保存されていることを前提としています。これは、作成/実行するのに問題がないはずです。できれば、データを生の形式で保存することもお勧めします。このような状況での操作が簡単になります。
ブロックを使用して、次の場所で並べ替えることができます。
sortedDatesArray = [[unsortedDatesArray sortedArrayUsingComparator: ^(id a, id b) {
NSDate *d1 = [NSDate dateWithString: s1];
NSDate *d2 = [NSDate dateWithString: s2];
return [d1 compare: d2];
}];
日付項目よりも多くの回数変換を行わないように、並べ替える前にすべての文字列を日付に変換することをお勧めします。どのソートアルゴリズムでも、配列内のアイテムの数よりも多くの文字列から日付への変換が得られます(場合によっては大幅に多くなります)
ブロックの並べ替えについてもう少し詳しく説明します:http ://sokol8.blogspot.com/2011/04/sorting-nsarray-with-blocks.html
を使用できますsortedArrayUsingFunction:context:
。サンプルは次のとおりです。
NSComparisonResult dateSort(NSString *s1, NSString *s2, void *context) {
NSDate *d1 = [NSDate dateWithString:s1];
NSDate *d2 = [NSDate dateWithString:s2];
return [d1 compare:d2];
}
NSArray *sorted = [unsorted sortedArrayUsingFunction:dateSort context:nil];
を使用するNSMutableArray
場合は、代わりに使用できますsortArrayUsingFunction:context:
。
私の場合、それが機能したのは次のとおりです。
NSArray * aUnsorted = [dataToDb allKeys]; NSArray * arrKeys = [aUnsortedsortedArrayUsingComparator:^ NSComparisonResult(id obj1、id obj2){ NSDateFormatter * df = [[NSDateFormatter alloc] init]; [df setDateFormat:@ "dd-MM-yyyy"]; NSDate * d1 = [df dateFromString:(NSString *)obj1]; NSDate * d2 = [df dateFromString:(NSString *)obj2]; return [d1 compare:d2]; }];
私は辞書を持っていました。そこでは、すべてのキーの日付がdd-MM-yyyyの形式になっています。また、allKeysは、ソートされていない辞書キーを返します。データを時系列で表示したかったのです。
を取得したら、 withをNSDate
作成し、を使用して並べ替えを行うことができます。NSSortDescriptor
initWithKey:ascending:
sortedArrayUsingDescriptors:
Swift 3.0
myMutableArray = myMutableArray.sorted(by: { $0.date.compare($1.date) == ComparisonResult.orderedAscending })
これを変える
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"beginDate" ascending:TRUE];
[myMutableArray sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[sortDescriptor release];
に
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Date" ascending:TRUE];
[myMutableArray sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[sortDescriptor release];
Date
キーを変更するだけです。常にキーを変更する必要があります