我们如何在ios中将Ascending设置为DateAndTime

时间:2016-02-19 03:54:27

标签: ios objective-c arrays

嗨,我是ios的新手,我正在将我的应用程序与服务集成,我在这里获取行程创建TimeandDate值并将该值存储为一个单独的数组,并且我从服务获取pickUpAdress值并将它们存储为一个单独的arrayList

在arrayList中存储后,我在tableList

中以“升序”顺序设置日期值

设置“升序”后,当我在tableList中显示它们彼此不匹配的数组列表数据时,请帮助我如何解决此问题如何设置我的Adrress数组值以匹配相关的DateandTimes

代码: -

#import "ViewController.h"

@interface ViewController ()
{
    NSSortDescriptor* sortOrder;
    NSArray * ToBeReadyTimeArray1;
    NSMutableArray * ToBeReadyTimeArray ,*finalToBeReadyTimeArray,*AdressArray;
    UITableView * MaintableView;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    ToBeReadyTimeArray1 = [[NSArray alloc]init];

    ToBeReadyTimeArray = [[NSMutableArray alloc]initWithObjects:@"02/17/2016",@"03/13/2016",@"04/18/2016",@"05/21/2016", nil];

    finalToBeReadyTimeArray =[[NSMutableArray alloc]init];

    AdressArray = [[NSMutableArray alloc]initWithObjects:@"Adress1",@"Adress2",@"Adress3",@"Adress4", nil];

    //Arange Date formate At Assending order:-

    sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"self" ascending: YES];
    ToBeReadyTimeArray1 =  [ToBeReadyTimeArray sortedArrayUsingDescriptors: [NSArray arrayWithObject: sortOrder]];

    for (NSString *dte in ToBeReadyTimeArray1){

        NSDateFormatter *aDateFormatter = [[NSDateFormatter alloc] init];
        [aDateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

        [aDateFormatter setDateFormat:@"MM/dd/yyyy"];
        NSDate * parsed = [aDateFormatter dateFromString:dte];
        [aDateFormatter setDateFormat:@"dd MMM yyyy"];
        NSString *aFormattedElapsedTime = [aDateFormatter stringFromDate:parsed];
        NSLog(@"tobe Ready time %@",aFormattedElapsedTime);
        [finalToBeReadyTimeArray addObject:aFormattedElapsedTime];
    }

}

1 个答案:

答案 0 :(得分:1)

我认为你试图用一些排序的顺序列出接送时间/地址。如果是这样,我会创建一个NSArray预订或旅行模型,其中包含地址(NSString *)和PickUpTime (NSDate *)等信息,然后使用该属性实现排序方法变量键。

就我而言,我实施了一系列具有pickUpTime和地址的预订模型。

@property (nonatomic, retain) NSNumber *pickUpTime; // Timestamp instead of NSDate ...
@property (nonatomic, retain) NSString *address;

我为NSArray实现了一个类别方法。请注意,排序顺序枚举是手动实现的。

+ (NSArray *)sortArray:(NSArray *)unSortedArray byOrder:(SortOrder)sortOrder usingVariableKey:(NSString *)variableKey {
    NSArray *sortedArray = [NSArray array];
    BOOL isAscendingOrder = NO;


    if (sortOrder == SortOrderAscending) {
        isAscendingOrder = YES;
    }

    if (unSortedArray) {
        NSSortDescriptor *valueDescriptor = [[NSSortDescriptor alloc] initWithKey:variableKey ascending:isAscendingOrder];
        NSArray *descriptors = [NSArray arrayWithObject:valueDescriptor];
        sortedArray = [[unSortedArray sortedArrayUsingDescriptors:descriptors] mutableCopy];
    }
    return sortedArray;
}

然后,我调用了以下内容......

    // Here, resultMutableArray is a mutable array of booking models ...
    NSArray *resultArray = [NSArray sortArray:[resultMutableArray copy] byOrder:SortOrderDescending usingVariableKey:@"pickUpTime"];