Objective C學習 陣列排序問題

2021-07-05 06:29:56 字數 3223 閱讀 7316

學到陣列的時候,經常為了資料排序因為排序問題煩惱,樓主也是如此,其實objc為我們提供了很好的排序機制,如下**加文本來解釋一下

//

// main.m

// nsarraysort (陣列排序)部落格

//// created by yuewen on 15/9/13.

// //首先定義乙個陣列

nsarray * array = @[@"boy",@"array",@"dary",@"car"];

/*** 用裡面元素自帶的方法進行排序,預設為公升序,因為是字串,所以我們可以用 nsstring 擁有的compare方法來進行預設排序

** @param compare: 字串的比較方法compare 返回的型別,在之前的陣列文章中也已經講過了

** @return 返回乙個接收的陣列

** 執行結果為: sortarray1 = @[@"array",@"boy",@"car",@"dary"]

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

這種比較相對的比較好用,舉個例子來看

/* 

如果是自定義的類進行比較呢 以 student 為例

我們預設有3個屬性

@property(nonatomic,strong)nsstring * name;

@property(nonatomic,strong)nsstring * studentid;

@property(nonatomic,strong)nsstring * age;

*///首先定義3個學生

student * student1 = [[student alloc]initwithname:@"張三" withstudentid:@"010" withage:@"12"];

student * student2 = [[student alloc]initwithname:@"張三" withstudentid:@"020" withage:@"11"];

student * student3 = [[student alloc]initwithname:@"刺客" withstudentid:@"008" withage:@"16"];

//新增到資料

nsarray * array2 = @[student1,student2,student3];

/*** 通過block**塊進行排序,我們可以自定義排序

** @param obj1 比較物件的第一物件

* @param obj2 比較物件的第二物件

** @return 返回的是比較的結果

** 執行結果是:sortarray2 = @[student3,student1,student2];

*/nsarray * sortarray2 = [array2 sortedarrayusingcomparator:^nscomparisonresult(id obj1, id obj2) ];

//如果我不想按照預設的公升序排呢,想降序,當然也是可以的

/*** 通過block**塊進行排序,我們可以自定義的降序排序

** @param obj1 比較物件的第一物件

* @param obj2 比較物件的第二物件

** @return 返回的是我們告知它排序的結果

** 執行結果是:sortarra2_1 = @[student3,student1,student2];

*/nsarray * sortarray2_1 = [array2 sortedarrayusingcomparator:^nscomparisonresult(id obj1, id obj2)

}];

其實現在看來,這種排序方法一般用來解決比較複雜的結構問題,比如復合排序問題,但是描述卻是很簡單的,看**量比第二種方法少了不少,一般的排序上面的兩種都可以解決的,但是為了好理解,用了乙個比較簡單的例項來展示一下,**如下:

/*

如果我們想先通過年齡排序,如果相同,我們再按照姓名進行排序呢

當然,這種排序也是可以的

那麼我們需要用到另一種型別: 叫做nssortdescriptor (排序描述)

*///首先我們為了驗證,我們需要改一下我們之前的student類物件

/*//首先定義3個學生

student * student1 = [[student alloc]initwithname:@"張三" withstudentid:@"010" withage:@"12"];

student * student2 = [[student alloc]initwithname:@"張三" withstudentid:@"009" withage:@"11"];

student * student3 = [[student alloc]initwithname:@"刺客" withstudentid:@"008" withage:@"16"];

那麼我們的需求是什麼呢,先按照名字公升序,如果名字相同,那麼我們就要看年齡

*///首先我們先把student加到陣列中

nsarray * array3 = @[student1,student2,student3];

/*** 然後就要定義我們的排序描述了

* 以下的意思就是說,我對比的憑據是 name 屬性, ascending 是否公升序排列

*/nssortdescriptor * descriptor1 = [nssortdescriptor sortdescriptorwithkey:@"name" ascending:yes];

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

/*** 我們為陣列新增排序描述

* 首先我們按照第1個描述來排列,如果相等,我們再會按照第2個排序描述來排序

** 執行結果是: sortarray3 = @[student3,student2,student1];

*/nsarray * sortarray3 = [array3 sortedarrayusingdescriptors:@[descriptor1,descriptor2]];

objective c 學習 陣列

oc裡面的陣列是一種線性結構資料型別,在foundation框架之內。分為可變陣列 nsarray 與不可變陣列 nsmultiarray 其中multiarray 繼承自 nsarray 所以nsarray的各種方法,nsmultiarray一樣可以使用。陣列可以通過下標索引到相應的物件。nsar...

Objective C陣列小結

objective c陣列相關操作。insert code here.nslog 陣列 指定多個字串建立陣列 nsarray array array nsarray arraywithobjects 0 asd 1 fds 2 哈咯 3 個人 nil 陣列的長度 nslog 陣列長度 d array...

Objective C陣列詳解

1 建立陣列並輸出。直接使用nsarray建立陣列,arr count 呼叫的是方法,來計算陣列的長度。arr objectatindex i 是呼叫方法獲取某個index位置的物件。int main int argc,char ar 2 此時呼叫計算長度使用的是陣列的屬性值count,所以直接使用...