数组: (2007-99 , 2001-96, 2005-93)
排序输出应为: (2005-93, 2001-96, 2007-99)
请帮帮我。
数组: (2007-99 , 2001-96, 2005-93)
排序输出应为: (2005-93, 2001-96, 2007-99)
请帮帮我。
您需要编写一个自定义比较器来执行此类操作。在下面的方法中,我使用 rangeOfString 获取破折号的位置,然后将子字符串从 1 位置开始进一步插入字符串,然后将其转换为 int 进行比较:
NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:@"2007-07",@"2005-01",@"2004-09",@"2003-02", nil];
NSArray *sortedArray = [arr sortedArrayUsingComparator: ^(NSString *s1, NSString *s2) {
if ([[s1 substringFromIndex:[s1 rangeOfString:@"-"].location + 1] intValue] > [[s2 substringFromIndex:[s2 rangeOfString:@"-"].location + 1] intValue]) {
return (NSComparisonResult)NSOrderedDescending;
}
if ([[s1 substringFromIndex:[s1 rangeOfString:@"-"].location + 1] intValue] < [[s2 substringFromIndex:[s2 rangeOfString:@"-"].location + 1] intValue]) {
return (NSComparisonResult)NSOrderedAscending;
}
return (NSComparisonResult)NSOrderedSame;
}];
NSLog(@"%@",sortedArray);
您可以使用自定义块对此进行排序(请注意,我假设您的所有数字格式正确):
NSArray *rollNumbers = [NSArray arrayWithObjects:@"2007-99", @"2001-96", @"2005-93", nil];
NSArray *sortedRollNumbers = [rollNumbers sortedArrayUsingComparator:^NSComparisonResult(NSString *roll1, NSString *roll2) {
NSArray *roll1Components = [roll1 componentsSeparatedByString:@"-"];
NSArray *roll2Components = [roll2 componentsSeparatedByString:@"-"];
NSNumber *roll1Number = [NSNumber numberWithInt:[[roll1Components objectAtIndex:1] intValue]];
NSNumber *roll2Number = [NSNumber numberWithInt:[[roll2Components objectAtIndex:1] intValue]];
return [roll1Number compare:roll2Number];
}];
NSLog(@"%@", sortedRollNumbers);
输出:
(“2005-93”、“2001-96”、“2007-99”)
您可以像这样对数组进行排序:
NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:@"2007-07",@"2005-01",@"2004-09",@"2003-02", nil];
NSMutableArray *marks = [[NSMutableArray alloc]init];
for (int i = 0; i < arr.count; i++)
{
NSArray *sep = [[arr objectAtIndex:i] componentsSeparatedByString:@"-"];
[marks addObject:[sep objectAtIndex:1]];
}
NSSortDescriptor *sortDesc = [[NSSortDescriptor alloc] initWithKey:@"" ascending:NO];
[marks sortUsingDescriptors:[NSArray arrayWithObject:sortDesc]];
NSMutableArray *sortedFinalArray = [[NSMutableArray alloc]init];
for (int i = 0; i < marks.count; i++)
{
for (int k = 0; k < arr.count; k++)
{
NSRange aRange = [[arr objectAtIndex:i] rangeOfString:[marks objectAtIndex:k]];
if (!(aRange.location == NSNotFound))
{
[sortedFinalArray addObject:[arr objectAtIndex:k]];
}
}
}
为了对数组进行排序,必须成对比较数组的元素以找出它们的顺序。您的特定排序是自定义的,因此您必须自己编写一个比较方法(例如命名compare:
),然后您可以使用它[arr sortUsingSelector:@selector(compare:)];
来对您的数组进行排序。
现在该compare:
方法必须对数组的元素是已知的,因为每个元素都使用它来将它与同一类的另一个元素进行比较。因此,您要么为实现 compare 方法的元素定义一个新类,要么将它们保留为 NSString,但是您必须定义一个实现 compare: 方法的类别。compare: 方法本身可能如下所示(伪代码):
-(NSComparisonResult) compare: (MyString *) myString {
if
(self.the_last_2_characters_interpreted_asNumber <
myString.the_last_2_characters_interpreted_asNumber)
return NSOrderedAscending;
else if
(self.the_last_2_characters_interpreted_asNumber ==
myString.the_last_2_characters_interpreted_asNumber)
return NSOrderedSame;
else
return NSOrderedDescending;
}