关于用Objective-C对NSMutableArray集合里面的数据进行排序的问题
存储在一个NSMutableArray集合里面,如何进行排序并输出成一个新集合啊?
排序规则:(a17>k16,a17+100>a17+90,a17+100>a17)
注:我将array里面的数据取出来封装成NSNumber对象后用冒泡排序进行比较要报错。给出详细代码的我会加分哈!
k123和k14打错了,应该是a123和a14,即都是“a”前缀开头,不好意思! 展开
-(void)test
{
//建立一个block,用来描述比较条件
NSComparator cp=^(NSString * obj1,NSString * obj2)
{
//首先,将字母a去掉
obj1=[obj1 substringFromIndex:1];
obj2=[obj2 substringFromIndex:1];
//然后,使用@“+”进行对字符串进行分割
NSArray * arr1=[obj1 componentsSeparatedByString:@"+"];
NSArray * arr2=[obj2 componentsSeparatedByString:@"+"];
//判断第一个数谁大
if ([arr1[0] integerValue]<[arr2[0] integerValue])
{
return NSOrderedAscending;
}
else if([arr1[0] integerValue]>[arr2[0] integerValue])
{
return NSOrderedDescending;
}
else//第一个数相同,就比较第二个数
{
//有可能数组里面只有一个元素,不可直接取第二个数
if (arr1.count==1)
{
if (arr2.count==1)
{
return NSOrderedSame;
}
else
{
return NSOrderedAscending;
}
}
else
{
if (arr2.count==1)
{
return NSOrderedDescending;
}
else
{
if([arr1[1] integerValue]>[arr2[1] integerValue])
{
return NSOrderedAscending;
}
else if([arr1[1] integerValue]>[arr2[1] integerValue])
{
return NSOrderedDescending;
}
else
{
return NSOrderedSame;
}
}
}
}
};
NSArray * arr=@[@"a123",@"a14", @"a17",@"a17+100",@"a19",@"a60",@"a83",@"a105+100",@"a109+120",@"a109+110",@"a109",@"a109+500"];
arr= [arr sortedArrayUsingComparator:cp];
for (NSString *obj in arr)
{
NSLog(@"%@",obj);
}
// 2014-02-21 17:15:38.652 SortArrayDemo[2867:70b] a14
// 2014-02-21 17:15:38.652 SortArrayDemo[2867:70b] a17
// 2014-02-21 17:15:38.652 SortArrayDemo[2867:70b] a17+100
// 2014-02-21 17:15:38.653 SortArrayDemo[2867:70b] a19
// 2014-02-21 17:15:38.653 SortArrayDemo[2867:70b] a60
// 2014-02-21 17:15:38.654 SortArrayDemo[2867:70b] a83
// 2014-02-21 17:15:38.654 SortArrayDemo[2867:70b] a105+100
// 2014-02-21 17:15:38.654 SortArrayDemo[2867:70b] a109
// 2014-02-21 17:15:38.655 SortArrayDemo[2867:70b] a109+120
// 2014-02-21 17:15:38.655 SortArrayDemo[2867:70b] a109+110
// 2014-02-21 17:15:38.656 SortArrayDemo[2867:70b] a109+500
// 2014-02-21 17:15:38.656 SortArrayDemo[2867:70b] a123
}
直接用正则表达式把所有数字匹配出来,再做比较应该是比较好的。
2014-02-16
Objective-C的数组比C++,Java的数组强大在于,NSArray保存的对象可以是不同的对象。但只能保存对象,int ,char,double等基本数据类型不能直接保存,需要通过转换成对象才能加入数组。
// 下面是忽略了a的值的算法
// 不清楚的到qq群209798263问
char * a_char[] = {"a123","a14","a17","a17+100","a19","a60","a83","a105+100","a109+120","a109+110","a109","a109+500"};
NSMutableArray *arr = [NSMutableArray array];
for (int i = 0; i < 12; i++) {
NSString *s = [NSString stringWithCString:a_char[i] encoding:NSUTF8StringEncoding];
[arr addObject:s];
}
NSLog(@"older arr = %@", arr);
NSArray *newArr = [arr sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSString *s1 = (NSString *)[[obj1 copy] lowercaseString];
NSString *s2 = (NSString *)[[obj2 copy] lowercaseString];
s1 = [s1 stringByReplacingOccurrencesOfString:@"a" withString:@""];
s2 = [s2 stringByReplacingOccurrencesOfString:@"a" withString:@""];
s1 = [s1 stringByReplacingOccurrencesOfString:@" " withString:@""];
s2 = [s2 stringByReplacingOccurrencesOfString:@" " withString:@""];
NSArray *arr1 = [s1 componentsSeparatedByString:@"+"];
NSArray *arr2 = [s2 componentsSeparatedByString:@"+"];
float value1 = 0;
for (NSString *s in arr1) {
value1 += [s floatValue];
}
float value2 = 0;
for (NSString *s in arr2) {
value2 += [s floatValue];
}
if (value1 > value2) {
return NSOrderedDescending;
}
if (value1 >= value2 && value1 <= value2) {
return NSOrderedSame;
}
return NSOrderedAscending;
}];
NSLog(@"new arr = %@", newArr);