Just a Computer Graphics Studio & My Life

剛好用到多重排序 (Multiple Sort),於是就來練習一下,沒想到Objective C都幫我寫好了!我只要會用就好⋯⋯還記得當時寫C/C++的ACM題目,bug殺好久~知道怎麼實作之後,拿來用更有感覺:)~

話就不多說,直接來看程式碼。我把dictionary塞到array中,再以key為排序基準,這兩個key排列組合成四種排序方法,執行結果真是有趣~之後想要排序多少個key都不是問題了!

 /**
 Theme: Multiple Sort
 IDE: Xcode 5
 Language: Objective C
 Date: 102/12/12
 Author: HappyMan
 Blog: https://cg2010studio.wordpress.com/
 */

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *nameKey = @"name";
    NSString *numberKey = @"number";
    NSArray *array = @[@{nameKey:@"HappyMax", numberKey:@"1"},
                       @{nameKey:@"HappyMan", numberKey:@"77"},
                       @{nameKey:@"HappyWoman", numberKey:@"7"},
                       @{nameKey:@"HappyMan", numberKey:@"1"},
                       @{nameKey:@"HappyWoman", numberKey:@"17"},
                       @{nameKey:@"HappyMan", numberKey:@"7"}
                       ];
    NSSortDescriptor *nameSortDescriptor = [[NSSortDescriptor alloc] initWithKey:nameKey ascending:YES];
    NSSortDescriptor *numberSortDescriptor = [[NSSortDescriptor alloc] initWithKey:numberKey ascending:YES];
    NSArray *descriptors1 = [NSArray arrayWithObjects:nameSortDescriptor, nil];
    NSArray *descriptors2 = [NSArray arrayWithObjects:numberSortDescriptor, nil];
    NSArray *descriptors3 = [NSArray arrayWithObjects:nameSortDescriptor, numberSortDescriptor, nil];
    NSArray *descriptors4 = [NSArray arrayWithObjects:numberSortDescriptor, nameSortDescriptor, nil];
    NSArray *sortedArray = [array sortedArrayUsingDescriptors:descriptors1];
    NSLog(@"sortedArray1:%@", sortedArray);
    sortedArray = [array sortedArrayUsingDescriptors:descriptors2];
    NSLog(@"sortedArray2:%@", sortedArray);
    sortedArray = [array sortedArrayUsingDescriptors:descriptors3];
    NSLog(@"sortedArray3:%@", sortedArray);
    sortedArray = [array sortedArrayUsingDescriptors:descriptors4];
    NSLog(@"sortedArray4:%@", sortedArray);
}

執行結果:

2013-12-13 00:59:21.509 HappyTest2[624:a0b] sortedArray1:(
{
name = HappyMan;
number = 77;
},
{
name = HappyMan;
number = 1;
},
{
name = HappyMan;
number = 7;
},
{
name = HappyMax;
number = 1;
},
{
name = HappyWoman;
number = 7;
},
{
name = HappyWoman;
number = 17;
}
)
2013-12-13 00:59:21.510 HappyTest2[624:a0b] sortedArray2:(
{
name = HappyMax;
number = 1;
},
{
name = HappyMan;
number = 1;
},
{
name = HappyWoman;
number = 17;
},
{
name = HappyWoman;
number = 7;
},
{
name = HappyMan;
number = 7;
},
{
name = HappyMan;
number = 77;
}
)
2013-12-13 00:59:21.511 HappyTest2[624:a0b] sortedArray3:(
{
name = HappyMan;
number = 1;
},
{
name = HappyMan;
number = 7;
},
{
name = HappyMan;
number = 77;
},
{
name = HappyMax;
number = 1;
},
{
name = HappyWoman;
number = 17;
},
{
name = HappyWoman;
number = 7;
}
)
2013-12-13 00:59:21.511 HappyTest2[624:a0b] sortedArray4:(
{
name = HappyMan;
number = 1;
},
{
name = HappyMax;
number = 1;
},
{
name = HappyWoman;
number = 17;
},
{
name = HappyMan;
number = 7;
},
{
name = HappyWoman;
number = 7;
},
{
name = HappyMan;
number = 77;
}
)

因為比較的對象是string,所以原則上是比較ASCII code的順序,注意這裡的數字屬於字串。

參考:How to sort NSMutableArray using sortedArrayUsingDescriptors?

Advertisements

發表留言

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

您的留言將使用 WordPress.com 帳號。 登出 / 變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 / 變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 / 變更 )

Google+ photo

您的留言將使用 Google+ 帳號。 登出 / 變更 )

連結到 %s

標籤雲

%d 位部落客按了讚: