iOS開發 常用排序

2021-09-02 11:21:16 字數 1051 閱讀 9711

1.針對陣列直接倒序

// 倒序

nsmutablearray *tempmarr =

[nsmutablearray arraywithobjects:@"c"

,@"d"

,@"a"

,@"b"

,nil]

;tempmarr =

(nsmutablearray *)[

[tempmarr reverseobjectenumerator] allobjects]

;

1.針對資料模型 某個屬性進行排序

// 公升序 yes  降序 no

nssortdescriptor *sortdescriptor =

[nssortdescriptor sortdescriptorwithkey:@"sort" ascending:

yes]

;//這個陣列儲存的是排序好的物件

nsarray *temparray =

[array sortedarrayusingdescriptors:

[nsarray arraywithobject:sortdescriptor]];

// 如果排序屬性值一樣, 根據第二個屬性繼續排序

nssortdescriptor *sortdescriptor1 =

[nssortdescriptorsortdescriptorwithkey:@"sort"ascending:

yes]

;nssortdescriptor *sortdescriptor2 =

[nssortdescriptorsortdescriptorwithkey:@"price"ascending:

yes]

;nsarray *temparray =

[array sortedarrayusingdescriptors:

[nsarrayarraywithobjects:sortdescriptor1, sortdescriptor2, nil]

];

ios 開發 NSArray 排序

針對nsstring字串的排序 方法一 nsarray ary a3 a1 a2 a10 a24 nslog ary nsarray myary ary sortedarrayusingcomparator nsstring obj1,nsstring obj2 nslog myary 結果 a3,...

IOS開發常用巨集

字串是否為空 define kstringisempty str str iskindofclass nsnull class str nil str length 1 yes no 陣列是否為空 define karrayisempty array array nil array iskindof...

iOS開發常用巨集

大家都是知道使用巨集不僅方便,而且可以提高開發效率。下面總結了ios開發過程中的一些常用巨集,會持續的往裡面新增。字串是否為空 define kstringisempty str str iskindofclass nsnull class str nil str length 陣列是否為空 def...