TRStudent.h文件中代码
- #import <Foundation/Foundation.h>
- @interface TRStudent : NSObject
- @property(nonatomic,assign)int age;
- @property(nonatomic,copy)NSString *name;
- -(id)initWithAge:(int)age andName:(NSString*)name;
- +(TRStudent*)studentWithAge:(int)age andName:(NSString*)name;
- -(NSComparisonResult)compare:(TRStudent*)otherStudent;
- @end
复制代码 TRStudent.m文件中代码- #import "TRStudent.h"
- @implementation TRStudent
- -(NSUInteger)hash{
- NSLog(@"hash方法执行了");
- return [super hash];
- //return 1;
- }
- -(BOOL)isEqual:(id)object{
- NSLog(@"isEqual方法执行了");
- return [super isEqual:object];
- /*
- //1.自反性
- if(self == object){//两个引用指向同一个对象
- return YES;
- }else{
- //2.类型是否相同 如果类型不相同 肯定不同
- if(![object isMemberOfClass:[TRStudent class]]){
- return NO;
- }else{//两个对象的类型相同
- //3.才比较对象的值
- TRStudent *otherStu = object;
- if (self.age==otherStu.age&&[self.name isEqualToString:otherStu.name]) {
- return YES;
- }else{
- return NO;
- }
- }
- }
- return NO;
- */
- }
- -(id)initWithAge:(int)age andName:(NSString*)name{
- self = [super init];
- if (self) {
- self.age = age;
- self.name = name;
- }
- return self;
- }
- +(TRStudent*)studentWithAge:(int)age andName:(NSString*)name{
- return [[TRStudent alloc]initWithAge:age andName:name];
- }
- -(NSComparisonResult)compare:(TRStudent*)otherStudent{
- return [self.name compare:otherStudent.name];
- }
- @end
复制代码main.m文件中的代码 - <font color="#bb2ca2" style="font-size: 13px;">#import <Foundation/Foundation.h></font>
- <font color="#bb2ca2" style="font-size: 13px;">#import "TRStudent.h"</font>
- <font color="#bb2ca2" style="font-size: 13px;">int main(int argc, const char * argv[])</font>
- <font color="#bb2ca2" style="font-size: 13px;">{</font>
- <font color="#bb2ca2" style="font-size: 13px;"> @autoreleasepool {</font>
- <font color="#bb2ca2" style="font-size: 13px;"> TRStudent *stu1 = [TRStudent studentWithAge:18 andName:@"zhangsan"];</font><font color="#ff0000" size="4">1.新建各个学生对象</font>
- <font color="#bb2ca2" style="font-size: 13px;"> TRStudent *stu2 = [TRStudent studentWithAge:19 andName:@"lisi"];</font>
- <font color="#bb2ca2" style="font-size: 13px;"> TRStudent *stu3 = [TRStudent studentWithAge:20 andName:@"wangwu"];</font>
- <font color="#bb2ca2" style="font-size: 13px;"> TRStudent *stu4 = [TRStudent studentWithAge:21 andName:@"zhaoliu"];</font>
- <font color="#bb2ca2" style="font-size: 13px;"> NSDictionary *stus = [NSDictionary dictionaryWithObjectsAndKeys:stu1,stu1.name,stu2,stu2.name,stu3,stu3.name,stu4,stu4.name, nil];</font><font size="4" color="#ff0000">2.将新建的学生对象放入一个字典中</font>
- <font color="#bb2ca2" style="font-size: 13px;"> //排序前</font><font color="#ff0000" size="4">3.1第一种排序方法</font>
- <font color="#bb2ca2" style="font-size: 13px;"> NSArray *allKeys = [stus allKeys];</font>
- <font color="#bb2ca2" style="font-size: 13px;"> for (NSString *key in allKeys) {</font>
- <font color="#bb2ca2" style="font-size: 13px;"> //key->value</font>
- <font color="#bb2ca2" style="font-size: 13px;"> TRStudent *stu = stus[key];</font>
- <font color="#bb2ca2" style="font-size: 13px;"> NSLog(@"name:%@ age:%d",stu.name,stu.age);</font>
- <font color="#bb2ca2" style="font-size: 13px;"> }</font>
- <font color="#bb2ca2" style="font-size: 13px;"> //排序后</font>
- <font color="#bb2ca2" style="font-size: 13px;"> //1.排序的第一种方式 无法对字典进行排序 只能对key进行排序</font>
- <font color="#bb2ca2" style="font-size: 13px;"> /*</font>
- <font color="#bb2ca2" style="font-size: 13px;"> NSArray *sortedAllKeys = [allKeys sortedArrayUsingSelector:@selector(compare:)];</font>
- <font color="#bb2ca2" style="font-size: 13px;"> for (NSString *key in sortedAllKeys) {</font>
- <font color="#bb2ca2" style="font-size: 13px;"> //key->value</font>
- <font color="#bb2ca2" style="font-size: 13px;"> TRStudent *stu = stus[key];</font>
- <font color="#bb2ca2" style="font-size: 13px;"> NSLog(@"name:%@ age:%d",stu.name,stu.age);</font>
- <font color="#bb2ca2" style="font-size: 13px;"> }</font>
- <font color="#bb2ca2" style="font-size: 13px;"> */</font>
- <font color="#bb2ca2" style="font-size: 13px;"> //2.字典排序方式2 </font><font color="#ff0000" size="4">3.2第二种排序方法</font>
- <font color="#bb2ca2" style="font-size: 13px;"> NSArray *sortedAllKeys = [stus keysSortedByValueUsingSelector:@selector(compare:)];</font>
- <font color="#bb2ca2" style="font-size: 13px;"> for (NSString *key in sortedAllKeys) {</font>
- <font color="#bb2ca2" style="font-size: 13px;"> //key->value</font>
- <font color="#bb2ca2" style="font-size: 13px;"> TRStudent *stu = stus[key];</font>
- <font color="#bb2ca2" style="font-size: 13px;"> NSLog(@"name:%@ age:%d",stu.name,stu.age);</font>
- <font color="#bb2ca2" style="font-size: 13px;"> }</font>
- <font color="#bb2ca2" style="font-size: 13px;"> </font>
- <font color="#bb2ca2" style="font-size: 13px;"> NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@1,@"one",@2,@"two", nil];</font>
- <font color="#bb2ca2" style="font-size: 13px;"> [dic writeToFile:@"/Users/yr/Desktop/dic.xml" atomically:NO];</font><font color="#ff0000" size="4">4.将排序好的对象写进dic.xml文件中</font>
- <font color="#bb2ca2" style="font-size: 13px;"> </font>
- <font color="#bb2ca2" style="font-size: 13px;"> }</font>
- <font color="#bb2ca2" style="font-size: 13px;"> return 0;</font>
- <font color="#bb2ca2" style="font-size: 13px;">}</font>
复制代码 一共有两种排序方法,在TRStudent.h和TRStudent.m文件中建立一个student对象,.h文件是对该对象属性方法的申明,.m文件是对该对象具体属性和方法的实现,然后在main.m中新建该对象,对他进行赋值,从而得到对字典中的key的排序的一个范例
|
|