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

2021-06-22 16:00:06 字數 1004 閱讀 4662

第一種,利用陣列的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 cmptr id obj1,id obj2 if obj1 integervalue obj2 integervalue return ...

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

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

幾種簡單的排序

今天檢視了arrays.sort a 的源 發現好複雜,根據a的長度不同,arrays.sort a 會呼叫不同的排序方法。簡單複習了下 快速排序 很簡單,把比某個數大的放右邊,小的放左邊。private static int partition int n,int left,int right i...