-4

がありNSMutableArray、日付で降順に並べ替えたいと思います。日付は次のようになります。

金曜日、2012 年 12 月 7 日 08:40:33 +0100

4

1 に答える 1

1

配列に日付のみがある場合は、次の基準で並べ替えることができます。

NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"self" ascending: NO];
return [myArray sortedArrayUsingDescriptors: [NSArray arrayWithObject: sortOrder]];

またはブロックを使用する

NSArray *reverseOrderUsingComparator = [dateArray sortedArrayUsingComparator: 
                                       ^(id obj1, id obj2) {
                                           return [obj2 compare:obj1];
                                       }];

編集:

議論に従って、コードを確認してください:

これらのプロパティを持つクラスがあると仮定します:

//MyClass.h
@interface MyClass : NSObject

@property(strong) NSString *title;
@property(strong) NSString *description;
@property(strong) NSDate *pubDate;

@end 

//MyClass.m
@implementation MyClass
- (id)init
{
    self = [super init];
    if (self) {
        _title=[NSString new];
        _description=[NSString new];
        _pubDate=[NSDate new];
    }
    return self;
}

-(NSComparisonResult)comparePubDate:(MyClass *)pd{
    return [_pubDate compare:[pd pubDate]];
}
@end

オブジェクトの配列があるクラスで

.h で

@property(strong) NSMutableArray *myClassObjects;

.m

-(NSDate *)dateInMyFormat:(NSString *)dateString{
    NSDateFormatter *dateFormatter = [NSDateFormatter new];
    [dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss z"];
    [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"EN"]];
    NSDate *myDate=[dateFormatter dateFromString:dateString];

    return myDate;    
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{

    MyClass *obj1=[MyClass new];
    [obj1 setTitle:@"obj1 title"];
    [obj1 setDescription:@"obj1 description"];
    [obj1 setPubDate:[self dateInMyFormat:@"Fri, 07 Dec 2012 08:40:33 +0100"]];

    MyClass *obj2=[MyClass new];
    [obj2 setTitle:@"obj2 title"];
    [obj2 setDescription:@"obj2 description"];
    [obj2 setPubDate:[self dateInMyFormat:@"Fri, 08 jan 2012 08:40:33 +0100"]];

    MyClass *obj3=[MyClass new];
    [obj3 setTitle:@"obj3 title"];
    [obj3 setDescription:@"obj3 description"];
    [obj3 setPubDate:[self dateInMyFormat:@"Fri, 08 jan 2012 06:40:33 +0100"]];

    MyClass *obj4=[MyClass new];
    [obj4 setTitle:@"obj4 title"];
    [obj4 setDescription:@"obj4 description"];
    [obj4 setPubDate:[self dateInMyFormat:@"Fri, 01 Jan 2012 08:40:33 +0100"]];

    [_myClassObjects addObject:obj1];
    [_myClassObjects addObject:obj2];
    [_myClassObjects addObject:obj3];
    [_myClassObjects addObject:obj4];

    for (MyClass *object in _myClassObjects) {
        NSLog(@"Title:%@, %@, %@",object.title, object.description, object.pubDate);
    }

    //for sorting
    NSArray *sortedArray=[_myClassObjects sortedArrayUsingSelector:@selector(comparePubDate:)];
    _myClassObjects=(NSMutableArray *)sortedArray;

    NSLog(@"After sorting");
    for (MyClass *object in _myClassObjects) {
        NSLog(@"Title:%@, %@, %@",object.title, object.description, object.pubDate);
    }
}
于 2012-12-09T08:46:17.057 に答える