LoginSignup
0
0

More than 5 years have passed since last update.

ObjC to Swift : Sorting objects in an array by date

Last updated at Posted at 2015-05-10

Objective-C

//  object being sorted
@interface ObjCObject : NSObject
@property (copy) NSString * name;
@property (strong) NSDate * date;

//  mutable array of the object
NSMutableArray * objects = ...

//  sort by object.date
[objects sortUsingComparator: ^NSComparisonResult( ObjCObject * object0,
                                                   ObjCObject * object1 )
{
    return [object0.date compare: object1.date];
}];

Swift

//  object being sorted
class SwiftObject
{
    var name = String()
    var date = NSDate()
}

//  mutable array of the object
var objects = ...

//  sort by object.date
objects.sort
{
    $0.date.timeIntervalSince1970 > $1.date.timeIntervalSince1970
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0