15.Object-C--NSArray数组的排序

NSArray数组的排序有三种方式:

1、简单排序(sortedArrayUsingSelector:)

2、利用block语法(sortedArrayUsingComparator:)

3、高级排序(sortedArrayUsingDescriptors:)

1、简单排序(sortedArrayUsingSelector:)

如果只是对字符串的排序,可以利用sortedArrayUsingSelector:方法就可以了,代码如下:

 1 #pragma mark 数组排序 sortedArrayUsingSelector
 2
 3 void arraySort1()
 4 {
 5     NSArray *array = [NSArray arrayWithObjects:@"6",@"9",@"1",@"2",nil];
 6     //返回一个排好序的数组,原来的数组不会变 (SEL)中传递的是排序的规则。@selector(compare:)
 7     //制定元素的比较方法 compare
 8     NSArray *arrayNew = [array sortedArrayUsingSelector:@selector(compare:)];
 9     NSLog(@"arrayNew :%@",arrayNew);
10 }

这里是制定使用Compare方法进行排序。我们也可以制定自己的排序方法

 1 #import <Foundation/Foundation.h>
 2 @class Book;
 3 @interface Student : NSObject
 4 @property(nonatomic,retain)NSString *firstName;
 5 @property(nonatomic,retain)NSString *lastName;
 6 @property(nonatomic,retain)Book *book;
 7 //快速创建student
 8 + (id)studentWithFirstName:(NSString*)firstName lastName:(NSString*)lastName;
 9 //比较规则
10 - (NSComparisonResult)compareStudent:(Student*)stu;
11 @end
12
13 #import "Student.h"
14 #import "Book.h"
15 @implementation Student
16 + (id)studentWithFirstName:(NSString*)firstName lastName:(NSString*)lastName
17 {
18     //类方法自动释放所以使用autorelease
19     Student *stu = [[[Student alloc]init]autorelease];
20     stu.firstName = firstName;
21     stu.lastName = lastName;
22     return stu;
23 }
24
25 - (NSComparisonResult)compareStudent:(Student*)stu
26 {
27     //1.先按照姓排序
28     NSComparisonResult result = [self.lastName compare:stu.lastName];
29
30     if(result == NSOrderedSame)
31     {
32         result = [self.firstName compare:stu.firstName];
33     }
34     return result;
35 }
36 - (NSString *)description
37 {
38     return  [NSString stringWithFormat:@"[%@ %@]",self.lastName,self.firstName];
39 }
40
41 - (void)dealloc
42 {
43     [_book release];
44     [_firstName release];
45     [_lastName release];
46     [super dealloc];
47 }
48 @end
49
50
51 #pragma mark 数组排序 arraySort2
52 void arraySort2()
53 {
54     Student *stu = [Student studentWithFirstName:@"QY" lastName:@"Ma"];
55     Student *stu1 = [Student studentWithFirstName:@"jf" lastName:@"Jia"];
56     Student *stu2 = [Student studentWithFirstName:@"sy" lastName:@"Qu"];
57     NSArray *array = [NSArray arrayWithObjects:stu,stu1,stu2,nil];
58     //指定排序的方法
59     NSArray *arrayNew = [array sortedArrayUsingSelector:@selector(compareStudent:)];
60     NSLog(@"arrayNew : %@",arrayNew);
61
62 }

2.利用block语法(sortedArrayUsingComparator:)

这是苹果官方提供的block方法,其中数组排序可以用sortedArrayUsingComparator:方法

 1 void arraySort3()
 2 {
 3     Student *stu = [Student studentWithFirstName:@"QY" lastName:@"Ma"];
 4     Student *stu1 = [Student studentWithFirstName:@"jf" lastName:@"Jia"];
 5     Student *stu2 = [Student studentWithFirstName:@"sy" lastName:@"Qu"];
 6     NSArray *array = [NSArray arrayWithObjects:stu,stu1,stu2,nil];
 7     //利用block进行排序
 8     NSArray *arrayNew =   [array sortedArrayUsingComparator:^NSComparisonResult(Student *obj1, Student *obj2) {
 9         //1.先按照姓排序
10         NSComparisonResult result = [obj1.lastName compare:obj2.lastName];
11
12         if(result == NSOrderedSame)
13         {
14             result = [obj1.firstName compare:obj2.firstName];
15         }
16         return result;
17     }];
18     NSLog(@"arrayNew : %@",arrayNew);
19 }

3.高级排序(sortedArrayUsingDescriptors:)

如果是这样一种情况呢?Student类里有另外一个类的变量,比如说Student类除了lastName,firstName变量,还有一本书,Book类里有个name属性。对Student对象进行排序,有这样的要求:按照Book的name排序,如果是同一本书,那么再按照lastName进行排序,如果lastName也相同,最后按照firstName进行排序。

代码如下:

  1 //Book
  2 #import <Foundation/Foundation.h>
  3
  4 @interface Book : NSObject
  5 @property(nonatomic,retain)NSString *name;
  6 + (id)bookWithName:(NSString*)name;
  7 @end
  8
  9 #import "Book.h"
 10
 11 @implementation Book
 12 + (id)bookWithName:(NSString*)name
 13 {
 14     Book *book = [[[Book alloc]init]autorelease];
 15     book.name = name;
 16     return book;
 17
 18
 19 }
 20 - (void)dealloc
 21 {
 22     [_name release];
 23     [super dealloc];
 24 }
 25 @end
 26
 27 //Student
 28 #import <Foundation/Foundation.h>
 29 @class Book;
 30 @interface Student : NSObject
 31 @property(nonatomic,retain)NSString *firstName;
 32 @property(nonatomic,retain)NSString *lastName;
 33 @property(nonatomic,retain)Book *book;
 34 //快速创建student
 35 + (id)studentWithFirstName:(NSString*)firstName lastName:(NSString*)lastName;
 36 //快速创建student
 37 + (id)studentWithFirstName:(NSString*)firstName lastName:(NSString*)lastName bookName:(NSString*)bookName;
 38 //比较规则
 39 - (NSComparisonResult)compareStudent:(Student*)stu;
 40 @end
 41
 42 #import "Student.h"
 43 #import "Book.h"
 44 @implementation Student
 45 + (id)studentWithFirstName:(NSString*)firstName lastName:(NSString*)lastName
 46 {
 47     //类方法自动释放所以使用autorelease
 48     Student *stu = [[[Student alloc]init]autorelease];
 49     stu.firstName = firstName;
 50     stu.lastName = lastName;
 51     return stu;
 52 }
 53
 54
 55 //快速创建student
 56 + (id)studentWithFirstName:(NSString*)firstName lastName:(NSString*)lastName bookName:(NSString*)bookName
 57 {
 58     Student *stu = [Student studentWithFirstName:firstName lastName:lastName];
 59     stu.book = [Book bookWithName:bookName];
 60     return stu;
 61
 62 }
 63 - (NSComparisonResult)compareStudent:(Student*)stu
 64 {
 65     //1.先按照姓排序
 66     NSComparisonResult result = [self.lastName compare:stu.lastName];
 67
 68     if(result == NSOrderedSame)
 69     {
 70         result = [self.firstName compare:stu.firstName];
 71     }
 72     return result;
 73 }
 74 - (NSString *)description
 75 {
 76     return  [NSString stringWithFormat:@"[%@ %@ %@]",self.lastName,self.firstName,self.book.name];
 77 }
 78
 79 - (void)dealloc
 80 {
 81     [_book release];
 82     [_firstName release];
 83     [_lastName release];
 84     [super dealloc];
 85 }
 86 @end
 87
 88 //将排序描述按照顺序存入到数组中
 89 #pragma mark 数组排序 sortedArrayUsingDescriptors 排序描述器
 90 void arraySort5()
 91 {
 92     Student *stu1 = [Student studentWithFirstName:@"QY" lastName:@"Ma" bookName:@"book3"];
 93     Student *stu2 = [Student studentWithFirstName:@"JF" lastName:@"Jia" bookName:@"book1"];
 94     Student *stu3 = [Student studentWithFirstName:@"SY" lastName:@"Qu" bookName:@"book4"];
 95     NSArray *array = [NSArray arrayWithObjects:stu1,stu2,stu3, nil];
 96     //放入到描述器里面
 97     NSSortDescriptor *bookNameDesc = [NSSortDescriptor sortDescriptorWithKey:@"book.name" ascending:YES];
 98     NSSortDescriptor *lastNameDesc = [NSSortDescriptor sortDescriptorWithKey:@"lastName" ascending:YES];
 99     NSSortDescriptor *firstName = [NSSortDescriptor sortDescriptorWithKey:@"firstName" ascending:YES];
100     NSArray *array1 = [NSArray arrayWithObjects:bookNameDesc,lastNameDesc,firstName,nil];
101     NSArray *arrayNew = [array sortedArrayUsingDescriptors:array1];
102     NSLog(@"arrayNew :%@",arrayNew);
103
104 }
时间: 2024-08-10 00:07:10

15.Object-C--NSArray数组的排序的相关文章

给object数组进行排序(排序条件是每个元素对象的属性个数)

从汤姆大叔的博客里看到了6个基础题目:本篇是第3题 - 给object数组进行排序(排序条件是每个元素对象的属性个数) 解题关键: 1.Array.sort的用法 2.object的属性数量的统计 解点1:Array.sort的用法 Array.sort可以为数组指定一个排序规则,一般用如下格式进行指定,代码如下: var arr = [10,6,0,4]; console.log( arr.sort() ); //按字符排序 0,10,4,6 console.log( arr.sort( fu

js中的数组对象排序

一.普通数组排序 js中用方法sort()为数组排序.sort()方法有一个可选参数,是用来确定元素顺序的函数.如果这个参数被省略,那么数组中的元素将按照ASCII字符顺序进行排序.如: var arr = ["a", "b", "A", "B"]; arr.sort(); console.log(arr);//["A", "B", "a", "b"

第六讲 Block块语法及Block与数组的排序,ios字面量的使用(源代码上传)

1 #import <Foundation/Foundation.h> 2 3 4 #import "Student.h" 5 6 int c =200; 7 int main(int argc, const char * argv[]) { 8 @autoreleasepool { 9 /* 10 ------------------------------------- 11 定义block变量的四种方法 12 -----------------------------

小康陪你学JAVA--------sort方法对数组进行排序

本篇和大家分享另一种数组的操作的方法——sort方法对数组进行排序. 范例:TestJava4_5.java 01 // 以下程序是数组的排序操作,在这里使用了sort方法对数组进行排序 02 import java.util.*; 03 public class TestJava4_5 04 { 05    public static void main(String[] args) 06  { 07       int a[] = {4,32,45,32,65,32,2} ; 08 09  

array_multisort—对多个数组或多维数组进行排序

From: http://www.cnblogs.com/lwbqqyumidi/archive/2013/01/31/2887188.html PHP中array_multisort可以用来一次对多个数组进行排序,或者根据某一维或多维对多维数组进行排序. 关联(string)键名保持不变,但数字键名会被重新索引. 输入数组被当成一个表的列并以行来排序——这类似于 SQL 的 ORDER BY 子句的功能.第一个数组是要排序的主要数组.数组中的行(值)比较为相同的话就按照下一个输入数组中相应值的

NSArray常用方法、排序及乱序

#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 数组的排序 // 1. 定义一个数字数组 NSArray *array = @[@(1), @(2), @(3), @(4), @(5), @(6), @(7), @(8), @(9)]; // 2. 对数组进行排

使用NSSortDescriptor对字符串数组进行排序

NSSortDescriptor 指定用于对象数组排序的对象的属性. 如果是Employee对象需要按照name来排序,就生成下面的descriptor NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:name ascending:YES]; 如果需要多个排序,比如先按name排序,再按入职日期排序.那就创建两个descriptor NSSortDescriptor *descriptor = [NSS

java数组随机排序实现代码

例一 代码如下 复制代码 import java.lang.Math;import java.util.Scanner;class AarrayReverse{ public static void main(String args[]) { int a[]=new int[20]; for(int i=0;i<=15;i++) { Scanner sca=new Scanner(System.in); System.out.println("请输数组元素a["+"]&

不可变数组或者可变数组进行排序

#import <Foundation/Foundation.h> NSInteger myCompare(id obj1,id obj2,void *context) { //不分大小写进行升序排序 //return [obj1 caseInsensitiveCompare:obj2]; return -[obj1 caseInsensitiveCompare:obj2]; } int main(int argc, const char * argv[]) { @autoreleasepoo