蘋果給出的例子:
nsarray *sortedarray = [array sortedarrayusingcomparator: ^(id obj1, id obj2)
if ([obj1 integervalue] < [obj2 integervalue])
return (nscomparisonresult)nsorderedsame;
}];
//第一種,利用陣列的sortedarrayusingcomparator呼叫 nscomparator ,obj1和obj2指的陣列中的物件
nscomparator cmptr = ^(id obj1, id obj2)
if ([obj1 integervalue] < [obj2 integervalue])
return (nscomparisonresult)nsorderedsame;
};nsarray *sortarray = [[nsarray alloc] initwithobjects:@"1",@"3",@"4",@"7",@"8",@"2",@"6",@"5",@"13",@"15",@"12",@"20",@"28",@"",nil];
//排序前
nsmutablestring *outputbefore = [[nsmutablestring alloc] init];
for(nsstring *str in sortarray)
nslog(@"排序前:%@",outputbefore);
[outputbefore release];
//第一種排序
nsarray *array = [sortarray sortedarrayusingcomparator:cmptr];
nsmutablestring *outputafter = [[nsmutablestring alloc] init];
for(nsstring *str in array)
nslog(@"排序後:%@",outputafter);
[outputafter release];
第二種 排序方法 利用sortedarrayusingfunction 呼叫 對應方法customsort,這個方法中的obj1和obj2分別是指數組中的物件。
nsinteger customsort(id obj1, id obj2,void* context)
if ([obj1 integervalue] < [obj2 integervalue])
return (nscomparisonresult)nsorderedsame;
}nsarray *sortarray = [[nsarray alloc] initwithobjects:@"1",@"3",@"4",@"7",@"8",@"2",@"6",@"5",@"13",@"15",@"12",@"20",@"28",@"",nil];
//排序前
nsmutablestring *outputbefore = [[nsmutablestring alloc] init];
for(nsstring *str in sortarray)
nslog(@"排序前:%@",outputbefore);
[outputbefore release];
nsarray *array = [sortarray sortedarrayusingfunction:customsort context:nil];
nsmutablestring *outputafter = [[nsmutablestring alloc] init];
for(nsstring *str in array)
nslog(@"排序後:%@",outputafter);
[outputafter release];
第三種 利用sortusingdescriptors呼叫nssortdescriptor
nssortdescriptor *sortdescriptor = [[nssortdescriptor alloc] initwithkey:@"price" ascending:no];//其中,price為陣列中的物件的屬性,這個針對陣列中存放物件比較更簡潔方便
nsarray *sortdescriptors = [[nsarray alloc] initwithobjects:&sortdescriptor count:1];
[_totalinfoarray sortusingdescriptors:sortdescriptors];
[_airlistview refreshtable:_totalinfoarray];
[sortdescriptor release];
[sortdescriptors release];
陣列的排序問題。
程式的版權和版本宣告部分 檔名稱 array.cpp 作 者 董萬鵬 完成日期 2012 年12月2 日 版本號 v1.0 輸入描述 無 問題描述 冒泡法排列陣列 include using namespace std void bubble sort int x,int num void outp...
陣列排序問題
牛客網上有一道很典型的陣列排序問題 先用計算機生成了n個1到1000之間的隨機整數 n 1000 對於其中重複的數字,只保留乙個,把其餘相同的數去掉。然後再把這些數從小到大排序 同乙個測試用例裡可能會有多組資料,希望大家能正確處理 我的做法比較常規 先儲存陣列,然後去重,之後排序輸出。編寫 incl...
C 的陣列排序問題。
今天學生要複習一下陣列排序的問題。何為何為陣列,就是用陣列的思想,解決排序問題。比如 6,5,7,4,2如何用陣列排序 include void main 定義變數陣列a,將數值6,5,7,4,2複製到陣列中。for int i 1 i 5 i for總迴圈,開始為1,結束為4。for int k ...