IOS幾種簡單有效的陣列排序方法

2021-06-07 04:32:25 字數 1006 閱讀 5969

//第一種,利用陣列的sortedarrayusingcomparator呼叫 nscomparator ,obj1和obj2指的陣列中的物件

nscomparator cmptr = ^(id obj1, id obj2)

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

return (nscomparisonresult)nsorderedsame;

};第二種 排序方法 利用sortedarrayusingfunction 呼叫 對應方法customsort,這個方法中的obj1和obj2分別是指數組中的物件。

nsinteger customsort(id obj1, id obj2,void* context)

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

return (nscomparisonresult)nsorderedsame;

}第三種 利用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];

IOS幾種簡單有效的陣列排序方法

第一種,利用陣列的sortedarrayusingcomparator呼叫 nscomparator obj1和obj2指的陣列中的物件 陣列進行排序 nscomparator cpmtr id obj1,id obj2 公升序 if obj1 integervalue obj2 integerva...

ios的幾種簡單有效的陣列排序方法

第一種,利用陣列的sortedarrayusingcomparator呼叫 nscomparator obj1和obj2指的陣列中的物件 nscomparator cmptr id obj1,id obj2 if obj1 integervalue obj2 integervalue return ...

mysql 幾種排序 MySql的幾種排序方式

資料排序的情況很常見,今天介紹一下mysql的幾種排序方式,是我最近有使用到的,希望能對大家有所幫助。這裡先建立一張普通的表,create table test1 id bigint 20 not null auto increment,name varchar 50 not null,date t...