我有一个关于排序的问题NSMutableArray.我可以使用sortedArrayUsingDescriptors:方法用对象对数组进行排序.
比如我有一个NSMutableArray的places,我有一个属性frequency(int值),我想降序排序上frequency,但我不知道如何正确使用它.
我把什么作为钥匙initWithKey?
我的对象place包含:
NSString * name;
NSString * address;
NSString * frequency;
NSString * type;
Run Code Online (Sandbox Code Playgroud)
NSMutableArray * places;
... populate array with objects ...
NSSortDescriptor * sortByFrequency =
[[[NSSortDescriptor alloc] initWithKey:@"????????" ascending:NO] autorelease];
NSArray * descriptors = [NSArray arrayWithObject:sortByFrequency];
NSArray * sorted = [x sortedArrayUsingDescriptors:descriptors];
Run Code Online (Sandbox Code Playgroud) 如何通过访问其NSDate属性对对象数组进行排序?
我知道可以[mutableArray sortUsingSelector:@selector(compare:)];在一个日期数组上使用,但是如何通过访问数组中每个元素的NSDate属性,按最早的日期到最新日期对对象进行排序?
我有一个字典数组,sortArrayUsingComparator根据键对它们进行排序;
listPerc = [listItem sortedArrayUsingComparator: ^(id a, id b) {
NSString *first = [a objectForKey:@"perc"];
NSString *second = [b objectForKey:@"perc"];
return [first compare:second];
Run Code Online (Sandbox Code Playgroud)
问题是这个键是一个数字,顺序是这样的例如:1,10,15,17,2,23等,它不计算数字的大小.我能怎么做?
我需要NSNumber从另一个生成一个排序的s 数组NSArray.我想用这个sortedArrayUsingComparator:方法.我在Apple文档中找到了这个例子:
NSArray *sortedArray = [array sortedArrayUsingComparator: ^(id obj1, id obj2) {
if ([obj1 integerValue] > [obj2 integerValue]) {
return (NSComparisonResult)NSOrderedDescending;
}
if ([obj1 integerValue] < [obj2 integerValue]) {
return (NSComparisonResult)NSOrderedAscending;
}
return (NSComparisonResult)NSOrderedSame;
}];
Run Code Online (Sandbox Code Playgroud)
这里我只是在块中提供比较器,这很好.排序本身 - 隐藏在我身上的一点,它使用了这个块 - 似乎是在提升.
我想知道我是否可以在此块外部请求不同的排序顺序,或者我是否只需要翻转<和>(或NSOrderedXXX)s?
我有一个NSArray具有name属性的对象.
我想通过过滤数组 name
NSString *alphabet = [agencyIndex objectAtIndex:indexPath.section];
//---get all states beginning with the letter---
NSPredicate *predicate =
[NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alphabet];
NSMutableArray *listSimpl = [[NSMutableArray alloc] init];
for (int i=0; i<[[Database sharedDatabase].agents count]; i++) {
Town *_town = [[Database sharedDatabase].agents objectAtIndex:i];
[listSimpl addObject:_town];
}
NSArray *states = [listSimpl filteredArrayUsingPredicate:predicate];
Run Code Online (Sandbox Code Playgroud)
但我得到一个错误 - "不能用不是字符串的东西做一个子串操作(lhs = <1,箭头> rhs = A)"
我怎样才能做到这一点?我想过滤数组中的第一个字母name为'A'.
我有一个NSMutableArray包含各种长度的NSStrings.我如何按字符串长度排序数组?
(抱歉长标题)
我有一个自定义对象Person,它又有一个NSSet名为Appointment的自定义对象.因此,一个人可以有几个约会.约会的值为startTime和endTime.
这些是核心数据NSMangagedObject类.
@interface Person : NSManagedObject
@property (nonatomic, retain) NSString *personName;
@property (nonatomic, retain) NSSet *appointments;
// etc
@end
@interface Appointment : NSManagedObject
@property (nonatomic, retain) NSNumber * startSecond;
@property (nonatomic, retain) NSNumber * endSecond;
// etc
@end
Run Code Online (Sandbox Code Playgroud)
我如何获得人员名单,按照他们任命中的最早开始时间顺序排列?
我只是好奇在一组点之间绘制简单路线的正确方法是什么?我目前有一个坐标数组,当我将它传递给polylineWithCoordinates并完成所有其他必要的事情时,它会绘制一个大的网络线,将所有点相互连接起来.我看了几个样本,但是他们似乎没有做任何特别的事情来解释这个,即使他们使用两个以上的点.
- (void)viewDidLoad
{
[super viewDidLoad];
...
//Add drawing of route line
CLLocationCoordinate2D coordinates[[myCheckpoints count]];
int i = 0;
for (Checkpoint *ckpt in myCheckpoints)
{
coordinates[i] = CLLocationCoordinate2DMake([ckpt.lat floatValue] , [ckpt.lon floatValue]);
i++;
}
MKPolyline *route = [MKPolyline polylineWithCoordinates: coordinates count: [myCheckpoints count]];
[mapView addOverlay:route];
}
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay {
MKPolylineView *polylineView = [[[MKPolylineView alloc] initWithPolyline:overlay] autorelease];
polylineView.strokeColor = [UIColor greenColor];
polylineView.lineWidth = 5.0;
return polylineView;
}
Run Code Online (Sandbox Code Playgroud)
这是我的mapViewController中负责绘图的代码,以防有人看到我正在做什么或不做什么.

现在我更仔细地看一切,实际上并没有将相邻的坐标彼此连接起来.每个点只有2条线来源于它将该点连接到另外2个点,但我无法弄清楚它连接它们的模式.
所以我有一个Foo包含许多成员的自定义类:
@interface Foo : NSObject {
NSString *title;
BOOL taken;
NSDate *dateCreated;
}
Run Code Online (Sandbox Code Playgroud)
在另一个类中,我有一个NSMutableArray包含这些对象的列表.我非常想根据dateCreated属性对这个数组进行排序; 我知道我可以为此编写自己的排序器(迭代数组并根据日期重新排列)但我想知道是否有一种正确的Objective-C方法来实现这一目标?
我可以提供成员变量排序的某种排序机制会很棒.
在C++中我曾经重载<=>运算符,这允许我按对象排序,但我有一种有趣的感觉Objective-C可能提供更好的选择?
非常感谢
我目前正在尝试对用户音乐收藏进行分类,并且能够获得用户最常播放的歌曲/艺术家将极大地改善应用中的用户体验.是否有可能获得比赛的数量?我该怎么做呢