iOS陣列排序 倒敘 生序 降序

2021-08-17 22:52:57 字數 4431 閱讀 4002

1.倒序

在ios開發的過程中,經常需要使陣列中的資料倒敘排列!比如在tableview顯示資料的時候需要使資料倒序排列!那麼如何解決陣列的倒序排列問題呢?好多開發的小夥伴可能是便利陣列的下標來獲取,但是這種方法如果資料很大的情況下程式體驗度會降低 , 這裡介紹乙個方法,一句話便可以搞定陣列的倒序排序問題!

//nsmutablearray *temp = [nsmutablearray arraywithobjects:@"5",@"1",@"4",@"2",nil];

temp = (nsmutablearray *)[[temp reverseobjectenumerator] allobjects];

2.公升序/ 降序

字串sortedarrayusingselector

//通過自帶的compare方法公升序排列

nsmutablearray *array = [[nsmutablearray alloc] initwithobjects:@"5",@"1",@"4",@"2",nil];

[array sortusingselector:@selector(compare:)];

//通過倒序的方法進行降序排列

nsenumerator *enumerator = [array reverseobjectenumerator];

array =[[nsmutablearray alloc]initwitharray: [enumerator allobjects]];

字典按key值大小對nsdictionary排序

sortedarrayusingselector

nsmutablearray *array = [nsmutablearray arraywithobjects:

[nsdictionary dictionarywithobjectsandkeys:@"obj0", [nsnumber numberwithint:0], nil],

[nsdictionary dictionarywithobjectsandkeys:@"obj5", [nsnumber numberwithint:5], nil],

[nsdictionary dictionarywithobjectsandkeys:@"obj2", [nsnumber numberwithint:2], nil],

[nsdictionary dictionarywithobjectsandkeys:@"obj3", [nsnumber numberwithint:3], nil],

[nsdictionary dictionarywithobjectsandkeys:@"obj1", [nsnumber numberwithint:1], nil],

[nsdictionary dictionarywithobjectsandkeys:@"obj4", [nsnumber numberwithint:4], nil], nil];

nsarray *resultarray = [array sortedarrayusingselector:@selector(compare:)];

//因為nsdictionary沒有compare的排序比較方法,所以需要我們自己寫乙個

- (nscomparisonresult)compare: (nsdictionary *)otherdictionary

sortedarrayusingcomparator

nsmutablearray *array = [nsmutablearray arraywithobjects:

[nsdictionary dictionarywithobjectsandkeys:@"obj0", [nsnumber numberwithint:0], nil],

[nsdictionary dictionarywithobjectsandkeys:@"obj5", [nsnumber numberwithint:5], nil],

[nsdictionary dictionarywithobjectsandkeys:@"obj2", [nsnumber numberwithint:2], nil],

[nsdictionary dictionarywithobjectsandkeys:@"obj3", [nsnumber numberwithint:3], nil],

[nsdictionary dictionarywithobjectsandkeys:@"obj1", [nsnumber numberwithint:1], nil],

[nsdictionary dictionarywithobjectsandkeys:@"obj4", [nsnumber numberwithint:4], nil], nil];

// nsarray *resultarray = [array sortedarrayusingselector:@selector(compare:)];

nsarray *resultarray = [array sortedarrayusingcomparator:^nscomparisonresult(id obj1, id obj2) ];

資料模型

sortedarrayusingdescriptors & sortusingdescriptors

前者帶返回值,是nsarray的方法,排好序的陣列是返回值中的陣列;

後者不帶返回值,是nsmutablearray的方法,是對當前陣列自己排序

接下來根據乙個物件的屬性,排列這個物件

.h@inte***ce person : nsobject

@property (nonatomic, retain) nsstring *name;

@property (nonatomic, assign) nsinteger age;

@end

.m@implementation person

@end

排序方法的實現

person *person1 = [[person alloc] init];

person1.name = @"loki";

person1.age = 25;

person *person2 = [[person alloc] init];

person2.name = @"mike";

person2.age = 22;

person *person3 = [[person alloc] init];

person3.name = @"larry";

person3.age = 33;

nsmutablearray *array = [nsmutablearray arraywithobjects:person1, person2, person3, nil];

//這裡類似kvo的讀取屬性的方法,直接從字串讀取物件屬性,注意不要寫錯

nssortdescriptor *sortdescriptor = [nssortdescriptor sortdescriptorwithkey:@"age" ascending:yes];

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

nsarray *temparray = [array sortedarrayusingdescriptors:[nsarray arraywithobject:sortdescriptor]];

for(nsinteger i = 0; i < [temparray count]; i++)

//下面是可變陣列的方法

// [array sortusingdescriptors:[nsarray arraywithobject:sortdescriptor]];

//

// for(nsinteger i = 0; i < [array count]; i++)

//

這裡的nsarray中的第一元素表示首先按照這個元素的公升序或者降序進行排序,對於有重複項的,再按照第二元素進行排序,依次進行類推

nssortdescriptor *sortdescriptor1 = [nssortdescriptorsortdescriptorwithkey:@"age"ascending:yes];

nssortdescriptor *sortdescriptor2 = [nssortdescriptorsortdescriptorwithkey:@"name"ascending:yes];

nsarray *temparray = [array sortedarrayusingdescriptors:[nsarrayarraywithobjects:sortdescriptor1, sortdescriptor2, nil]];

// 漢字排序

nsarray *result = [zimumarr sortedarrayusingselector:@selector(localizedcompare:)];

zimimarr是要排序的陣列 result是排序好的陣列

js 陣列快速排序(物件,數字)可正序可倒敘

quicksort arr,parm,sortsc let centerindex math.floor arr.length 2 獲取陣列中間的索引 let centervalue arr.splice centerindex,1 0 獲取陣列中間項 let left right let a ar...

簡單陣列公升降序排序

陣列的排序是經常碰到的事情,今天就總結一下簡單的呼叫api函式進行陣列排序,不涉及冒泡,插入,快速排序演算法之類的。首先公升序排序,直接呼叫arrays.sort 陣列 即可。降序排序 思路是先轉化為list,然後呼叫collections.sort 進行排序,之後呼叫collections.res...

集合 陣列正倒(公升降)序排序

1.list排序 正序 collections.sort list 倒序 collections.sort list collections.reverse list 注意collections.reverse list 是表示將該列表反過來 看 public static void main st...