2012-08-31 104 views
0

我想排序我的數組.... 在我的數組元素是在這個順序dateArray {0,1,2,3,4,5,6,7,8,9,10, 11,12,13,14,15,16,17,18,19,32,33,34,35,36,37,38,39,40,41}iPhone排序陣列

但我想這個數組以相反的順序 我用這個代碼逆轉..

dateArray=[[NSMutableArray alloc]initWithArray:[[date reverseObjectEnumerator] allObjects]]; 

但它給這個輸出

dateArray=(
9, 
8, 
7, 
6, 
5, 
4, 
3, 
2, 
19, 
18, 
17, 
16, 
15, 
14, 
13, 
12, 
11, 
10, 
1, 
0 
) 

但我想這個輸出dateArray(19,18,17,16,15,14,1 3,12,11,10 ........ 2,1,0) 我也使用排序方法,但輸出是一樣的。我怎樣才能做到這一點???請幫助

編輯:

我的數組元素是NSDocument目錄的內容....

解決 這個問題已經sovled。我不知道如何排序我的數組的確切方式,但我更改了我的子路徑名稱。它給右輸出..

由於提前

+0

它似乎是基於'NSString'而不是'NSNumber'進行排序。我建議你使用類似'sortUsingComparator:'的東西。 – Mazyod

+0

Thanx的答覆,我現在試試.. – Rox

+0

如果你想使用字符串而不是數字,你應該使用以下格式的正確排序:'20','19',...' 10','09','08',...'02','01','00',因此,您只需爲每個數字添加前導'0'。 – holex

回答

1

,如果你想使用字符串代替數字工作,你應該使用以下格式進行正確的排序:

2019,... 100908。 ... 0201,00

因此,你只需要添加領先的0到每一個數字。如果它們是字符串,那麼可以只將它們排序爲字符串,而不是數字,這就是爲什麼需要前導零。


的最無痛的解決方案是重命名的子路徑那麼,這裏的字符串來,你不需要改變任何東西在你的申請得到正確順序的方式。

1

你需要使用

-[NSArray sortedArrayUsingSelector:] 

-[NSMutableArray sortUsingSelector:],並通過@selector(比較:)作爲參數。

+0

啊,我沒有得到。 PLZ解釋如何做? – Rox

2

我假設你的數組包含對象而不是你的問題中的數字。以下是按升序或降序對數組排序的示例。

NSArray *array = [[NSArray alloc] initWithObjects:@"5",@"1",@"0",@"3",@"2",@"4", nil]; 

//or 

NSArray *array = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:5], [NSNumber numberWithInt:1], [NSNumber numberWithInt:0], [NSNumber numberWithInt:3], [NSNumber numberWithInt:2], [NSNumber numberWithInt:4], nil]; 


NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:nil ascending:NO]; //YES, for ascending 
NSLog(@"Sorted array %@",[array sortedArrayUsingDescriptors:[NSArray arrayWithObjects:sort, nil]]); 
+0

對不起,它給出了相同的輸出。 – Rox

+0

@Rox,看到這個編輯的數據!這個對我有用!讓我知道你的數據的類型是什麼,它們是對象(字符串)還是類型號碼? – Hemang

+0

它排序你的元素,但如果從1到20的UR元素,那麼它給這個輸出dateArray =(9,8,7,6,5,4,3,2,19,18,17,16,15,14, 13,12,11,10,1,0)...但我想像系列(20,19,... 2,1,0) – Rox

1

三種方式(其他很多,我相信)。 最簡單的方法(thanks to this SO question),是使用reverseObjectEnumerator:

NSArray *dateArrayReversed = [[dateArray reverseObjectEnumerator] allObjects]; 

或者你可以使用一個排序描述符(亂):

NSArray *arrayOne = [NSArray arrayWithObjects:@1,@2,@3,@4,@5,@6,@7,@8,@9,@10,@11,@12,@13,@14,@15,@16,@17,@18,@19, nil]; 

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

的另一種方法是創建對反式陣列的類別。請參閱this SO問題就是一個很好的例子。

How can I reverse a NSArray in Objective-C?

+0

我已經嘗試了上面兩種方式..但沒有得到我想要的.... – Rox

1

這可以很容易做到,你可以通過排序您NSSortDescriptor陣列,但有一個小竅門,我會展示給you.Here是代碼

NSMutableArray *yourArray=[[NSMutableArray alloc] initWithObjects:@"1",@"5",@"10",@"6",@"3",@"11", nil]; 

NSMutableArray *array=[[NSMutableArray alloc] init]; 
int count=[yourArray count]; 
for (int i=0; i<count; i++) { 
    [array addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:[[yourArray objectAtIndex:i]intValue]],@"val", nil]]; 
} 

if([array count] > 0){ 
    NSSortDescriptor * descriptor = [[[NSSortDescriptor alloc] initWithKey:@"val" 
                   ascending:NO] autorelease]; 
    NSArray * sortedArray = [array sortedArrayUsingDescriptors: 
          [NSArray arrayWithObject:descriptor]]; 
    [array removeAllObjects]; 
    [array addObjectsFromArray:sortedArray]; 
} 
[yourArray removeAllObjects]; 
[yourArray release]; 
NSLog(@"sorted array=%@",array); 

我在做這個是首先將字符串對象以NSNumber形式添加到可變字典中,然後將此字典添加到另一個數組中。現在使用NSSortDescriptor按升序或降序順序對數組進行排序。