2016-09-21 78 views
0

我有一個NSString的數組,每個NSString元素包含一個時間戳(epoch time)和其他字符,例如:"time:1474437948687, <other characters>"NSArray中的排序元素基於元素的時間戳

NSArrary *myData = [self loadData];// my array 

所以,myData看起來像這裏面:

{"time:1474437948687,fajlsfj...", 
"time:1474237943221, axsasdfd...", 
"time:1474681430940, someother...", 
... 
} 

我需要有一個包含相同的元素上面陣列的陣列,但在降時間戳的順序進行排序。我該怎麼做?

我卡住與迭代的NSString陣列上:

for (NSString element in myData) { 
... 
} 
+0

使用NSSortDescriptor基於時間對數組進行縮短。 –

+0

謝謝,我很快會看看NSSortDescriptor,我之前沒有用過它。 –

+0

你能告訴我們你的數組 –

回答

-3

所有這似乎是一個好主意,將字符串轉換爲反映在鍵 - 該數據的實體類型的實例的第一價值的態度。然後,你可以通過使用NSSortDescriptor一個實例很容易對其進行排序:

NSSortDescriptor *timeSorter = [NSSortDescriptor sortDescriptorWithKey:@"time" ascending:NO]; 
NSArray *sorted = [myData sortedArrayUsingSortDescriptors:@[timeSorter]]; 

但是,您可以通過使用更復雜的排序描述符對數組進行排序:

NSSortDescriptor *timeSorter = [NSSortDescriptor sortDescriptorWithKey:@"self" ascending:NO comparator: 
^(id one, id two) 
{ 
    NSString *timestamp1 = [one compenentsSepartedByString:@","][0]; 
    timestamp1 = [timestamp1 substringFromIndex:5]; 
    NSString *timestamp2 = [two compenentsSepartedByString:@","][0]; 
    timestamp2 = [timestamp2 substringFromIndex:5]; 
    return [timestamp1 compare:timestamp2 options:NSNumericSearch range:NSMakeRange(0, [timestamp1 length])]; 
}]; 
NSArray *sorted = [myData sortedArrayUsingSortDescriptors:@[timeSorter]]; 

鍵入Safari瀏覽器。

-1

使用以下sortedArrayUsingComparator它會爲我工作:我使用靜態數據,例如時間:1474437948687,..

**time:1474437948687, <other characters> Consider String Format..** 

    NSArrary *myData = [self loadData]; 
    NSArrary *sortedmyData = [[myData sortedArrayUsingComparator: ^(id obj1, id obj2) { 
     NSDateFormatter *df = [[NSDateFormatter alloc] init]; 
     // Change Date formate accordingly ==== 
     [df setDateFormat:@"dd-MM-yyyy"]; 
     NSDate *d1 = [df dateFromString:[self sperateDate:obj1]]; 
     NSDate *d2 = [df dateFromString:[self sperateDate:obj2]]; 
     return [d1 compare: d2]; 
    }]; 

//這是函數展開爲每時間:1474437948687,考慮字符串格式..

- (NSString *)sperateDate : (NSString *)obj1 { 
    NSArray *arr = [obj1 componentsSeparatedByString:@","]; 
    NSArray *arr1 = [arr[0] componentsSeparatedByString:@":"]; 
    return arr1[1]; 
} 

更新與原始類型比較:

NSArray *myData = @[@"time:1474437948687,fajlsfj...",@"time:1474237943221, axsasdfd...",@"time:1474681430940, someother..." 
    NSArray *sortedmyData = [myData sortedArrayUsingComparator: ^(id obj1, id obj2) { 
    NSNumber *d1 = [NSNumber numberWithDouble:[[self sperateDate:obj1] doubleValue]]; 
    NSNumber *d2 = [NSNumber numberWithDouble:[[self sperateDate:obj2] doubleValue]]; 
    return [d1 compare: d2]; 
    }]; 

希望這有助於你...

做讓我知道,如果您有任何其他查詢

+0

謝謝,我現在嘗試它,會讓你知道結果。 –

+0

@ Leem.fin感謝您的快速回復!有人被拒絕投票沒有檢查.. –

+0

我們可以只用毫秒的紀元時間做比較而不是轉換成NSDate嗎? –

-1

試試這個:

NSArray *timearray = @[@"time:1474437948687,fajlsfj...", 
     @"time:1474237943221, axsasdfd...", 
     @"time:1474681430940, someother..."]; 
    NSSortDescriptor *sortDescriptor; 

    sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"" 
               ascending:NO]; 

    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; 
    NSArray *sortedArray = [timearray sortedArrayUsingDescriptors:sortDescriptors]; 

    NSLog(@"sortedArray %@",sortedArray); 

輸出:

sortedArray (
     "time:1474681430940, someother...", 
     "time:1474437948687,fajlsfj...", 
     "time:1474237943221, axsasdfd..." 
    )