黑马程序员技术交流社区
标题:
关于字典中key的排序的自己的一些见解
[打印本页]
作者:
夜神月No1
时间:
2015-11-23 14:00
标题:
关于字典中key的排序的自己的一些见解
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的排序的一个范例
作者:
夜神月No1
时间:
2015-11-23 14:05
main.m文件中的代码我加了注释,导致解析的时候把样式也解析上去了,现在从新发一遍main.m中的文件
#import <Foundation/Foundation.h>
#import "TRStudent.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
TRStudent *stu1 = [TRStudent studentWithAge:18 andName:@"zhangsan"];
TRStudent *stu2 = [TRStudent studentWithAge:19 andName:@"lisi"];
TRStudent *stu3 = [TRStudent studentWithAge:20 andName:@"wangwu"];
TRStudent *stu4 = [TRStudent studentWithAge:21 andName:@"zhaoliu"];
NSDictionary *stus = [NSDictionary dictionaryWithObjectsAndKeys:stu1,stu1.name,stu2,stu2.name,stu3,stu3.name,stu4,stu4.name, nil];
//排序前
NSArray *allKeys = [stus allKeys];
for (NSString *key in allKeys) {
//key->value
TRStudent *stu = stus[key];
NSLog(@"name:%@ age:%d",stu.name,stu.age);
}
//排序后
//1.排序的第一种方式 无法对字典进行排序 只能对key进行排序
/*
NSArray *sortedAllKeys = [allKeys sortedArrayUsingSelector:@selector(compare:)];
for (NSString *key in sortedAllKeys) {
//key->value
TRStudent *stu = stus[key];
NSLog(@"name:%@ age:%d",stu.name,stu.age);
}
*/
//2.字典排序方式2
NSArray *sortedAllKeys = [stus keysSortedByValueUsingSelector:@selector(compare:)];
for (NSString *key in sortedAllKeys) {
//key->value
TRStudent *stu = stus[key];
NSLog(@"name:%@ age:%d",stu.name,stu.age);
}
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@1,@"one",@2,@"two", nil];
[dic writeToFile:@"/Users/yr/Desktop/dic.xml" atomically:NO];
}
return 0;
}
复制代码
作者:
来自山区的孩子
时间:
2015-11-23 15:58
帮顶,顺便问问楼主写的是实现是的.....
作者:
liruixue
时间:
2015-11-23 16:03
很用心,但表示没看懂
作者:
夜神月No1
时间:
2015-11-23 16:16
liruixue 发表于 2015-11-23 16:03
很用心,但表示没看懂
就是一些字典根据键值对进行排序
作者:
夜神月No1
时间:
2015-11-23 19:04
来自山区的孩子 发表于 2015-11-23 15:58
帮顶,顺便问问楼主写的是实现是的.....
写的很明了了
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2