陣列排序(按陣列中字典的某一鍵值對)

2022-09-20 03:54:07 字數 1308 閱讀 1043

1。普通陣列排序:

nsmutablearray *arr = [nsmutablearray arraywithobjects:@"0",@"8",@"6",@"1",nil];

nsarray *sortedarray = [arr sortedarrayusingcomparator:^nscomparisonresult(id obj1, id obj2)

if ([obj1 intvalue] < [obj2 intvalue])

return nsorderedsame;

}];nslog(@"排序後的陣列:%@",sortedarray);

2.陣列中有字典的排序:

nsmutablearray *mymutablearr = [[[nsmutablearray alloc] init] autorelease];

nsdictionary *dicone = [nsdictionary dictionarywithobjectsandkeys:@"1",@"price",@"2",@"number", nil];

nsdictionary *dictwo = [nsdictionary dictionarywithobjectsandkeys:@"6",@"price",@"5",@"number", nil];

nsdictionary *dicthree = [nsdictionary dictionarywithobjectsandkeys:@"3",@"price",@"1",@"number", nil];

nsdictionary *dicfour = [nsdictionary dictionarywithobjectsandkeys:@"4",@"price",@"3",@"number", nil];

[mymutablearr addobject:dicone];

[mymutablearr addobject:dictwo];

[mymutablearr addobject:dicthree];

[mymutablearr addobject:dicfour];

nsarray *sortdescriptors = [nsarray arraywithobject:[nssortdescriptor sortdescriptorwithkey:@"price" ascending:yes]];

[mymutablearr sortusingdescriptors:sortdescriptors];

nslog(@"排序後的陣列%@",mymutablearr);

3.對於中文的排序可以先將中文轉拼音首字母,拼音首字母裝換可以使用 pinyin.h 這個開源庫

二維陣列按指定的鍵值排序

二維陣列按指定的鍵值排序 param array 需要排序的二維陣列 param keys 需要排序根據的索引 param string type 正序 倒敘 預設倒序 return array string public function array sort array,keys,type as...

PHP 按二維陣列的某個鍵值排序

array keys array 返回所有鍵名 array values array 返回所有鍵值 result array reverse input 將陣列顛倒,不保留鍵名 result keyed array reverse input,true 將陣列顛倒,保留鍵名 array keys a...

根據物件陣列的某一屬性排序

今天做需求時候靈光一閃,想到了乙個很簡單的給物件陣列排序的方法,敲黑板 此排序非普通大眾排序 需求描述 有乙個物件陣列,大概長這樣 var bardata 渲染頁面時候呢,我們需要按照特定的順序來取資料並渲染。假如是按照name屬性來排序,並且順序為 掃碼e貸,誠e貸,稅源e貸,超值e貸,菸草e貸 ...