- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我很困惑为什么降序排序不适用于以下代码。我想通过前 5 名分数和其他逻辑来限制。分数将如下所示:22/30、12/18、34/38、23/32 等。我添加/删除了 SortDescriptor 以按降序排序,它似乎适用于前 3 个项目,但随后无法正确排序。有人可以帮忙吗?
- (NSMutableArray*) method1:(NSString *) mode byScore: (NSString *) score
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *array = [[defaults objectForKey:mode]mutableCopy];
if (!array)
{
array = [[NSMutableArray alloc] init];
}
NSLog(@"The content of array is%@", array);
if ([array count] < 5)
{
if (![array containsObject:score])
{
[array addObject:score];
// Need to sort here. But not too sure this is the right place
NSLog(@"The content of the sorted array upto 5 is%@", array);
}
}
else
{
if (![array containsObject:score])
{
if ([array lastObject] < score)
{
[array addObject:score];
// Need to sort here before I remove the last object
[array removeLastObject];
NSLog(@"The content of the sorted array else is%@",array);
}
}
}
[defaults setObject:array forKey:mode];
[defaults synchronize];
// I want the array in NSUserDefaults to be sorted in desc order
// don't know what to return here ==> the array object or the defaults object cast to NSMutableArray?
}
最佳答案
辅助函数
static NSComparisonResult CompareFloats( float a, float b )
{
if ( a < b ) { return NSOrderedAscending ; }
else if ( a > b ) { return NSOrderedDescending ; }
return NSOrderedSame ;
}
NSString 上的类别
@implementation NSString (Stuff)
-(float)floatValueForFraction
{
NSArray * components = [ self componentsSeparatedByString:@"/" ] ;
return [ components[0] floatValue ] / [ components[1] floatValue ] ;
}
@end
你的方法:
- (void)addScore:(NSString*)score forMode:(NSString*)mode
{
NSUserDefaults * defaults = [ NSUserDefaults standardUserDefaults ] ;
NSArray * scores = [ defaults objectForKey:mode ] ;
scores = scores ? [ scores arrayByAddingObject:score ] : @[ score ] ;
scores = [ scores sortedArrayUsingComparator:^(NSString * a, NSString * b){
return CompareFloats( [ a floatValueForFraction ], [ b floatValueForFraction ] ) ;
}]
if ( scores.count > 5 ) { scores = [ scores subarrayWithRange:(NSRange){ .length = 5 } ] ; }
[ default setObject:scores forKey:mode ] ;
}
如果您想在调用此方法后更新高分,只需使用 [ [ NSUserDefaults standardUserDefaults ] objectForKey:<mode> ]
.最好让你的方法只做一件事。
关于objective-c - NSUSerDefaults 与 NSMutableArray 按 desc 顺序排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14088921/
我有一个小程序,我不想再在浏览器中运行,而是想通过 JNLP 作为可下载、自动更新、可离线的 Java 应用程序运行。 我不想保留将其作为小程序运行的能力。 无论是将现有的小程序包装在 JFrame
我认为我没有做任何过于复杂的事情。我正在对一个大型 csv 数据文件进行预排序,因为它充满了按随机时间顺序到达的数据。索引正确,但返回格式不正确。 sortedList=sorted(read
我在GraphicalLayout中添加了一个ImageView,但它在我的实际设备上看起来与在AVD上不同。。我发现我需要在一个布局.xml中添加Content Description,但是当我添加
表格如下: CREATE TABLE `api_stats` ( `id` int(11) NOT NULL AUTO_INCREMENT, `ip` varchar(30) DEFAULT NU
我的表有百万条记录。我只想提取最后 200 行中计数器最多的 5 行。 我正在使用它,但我认为这是两次选择.. 这可能不适合最佳性能,请提出建议。 "select * from (
我正在尝试为 hg log 创建一个自定义模板,其中一部分显示第一行的前 N(例如 72)个字符。基于this answer到目前为止我收到的另一个问题: hg log --template '{de
这里我们有一个带连字符的字符串,如 0-1-3.... 并且长度不固定,我们在hive中还有一个DETAIL表来解释每个代码的含义。 详情 |代码 |描述 | + ---- + ---- + | 0
它应该列出一个列表,并在顶部列出最高百分比..但它没有 https://gyazo.com/ecde864ef09115b8b119eba8a39ecd68这是我运行时的图片 这里是代码。怎么了? $
我正在尝试根据论坛 ID 获取所有主题。这些主题需要首先按粘性排序,然后按最后发布日期排序。 我有这个查询,工作得几乎正常,但它没有按照我想要的方式对主题进行排序。 SELECT forum_
我在数据库中创建了一个表并添加了一些约束。当我在表上使用 DESC 关键字时,根据我放置的约束,“Key”列会更改其记录。 这是表定义: CREATE TABLE t( sif INT, sif
我有一个获取两行的查询 - SELECT tb.type, tb.value, tb.comp, tb.
我在 SQL 中有这个查询 SELECT Book FROM Library ORDER BY Book desc 我想要的排序 [Expired]Web Publishing Co., Ltd. [
我正在使用 DataTables 来显示一些数据,它效果很好,但我想稍微自定义它,但不确定如何。 我想要做的是,当用户单击列标题对该列进行排序时,我希望它最初按降序而不是升序排序。有什么办法可以做到这
我有这个 SQL(SQLite 数据库),它正确地从按 session ID 分组的消息表中获取最新消息,其中消息的 session ID 不是“无”。 SELECT * from messages
我想要 desc 表;操作以显示列的注释。我已经看到有些人做到了这一点,但我不知道如何。也许这取决于 SQL Developer 版本,我的是 2.1.0.63。数据库是Oracle 11g。 这是我
我正在检索 SQL 中的一些数据,按 DESC 排序。然后我想反转结果。我是通过将数据插入数组然后使用 array_reverse 来做到这一点的,但我发现这对 CPU 时间来说非常繁重,并且只想使用
我正在按日期降序从数据库中获取一些数据,并使用shift-push通过循环将其添加到数组中,因为新数据不断定期到达。 我无法理解的一件奇怪的事情是为什么第一个获取的值被添加到最后一个索引。 这是我的代
我有一个问题,我不知道如何解决。我有这个查询: $sql = "SELECT * FROM transfer_centre WHERE status = 'out' ORDER BY pris DES
我有 2 个 MYSQL 表,firma 和 rach 公司表: id_fir | nazwa | opis | nr_konta | logo-------------------------
我希望得到一些帮助:我正在尝试输出一个用户可以通过各种选择字段查看和排列的 mysql 数据表。我希望数据按日期顺序自动排列(最新的在前)并且每页限制为 10 个结果。 我有以下脚本摘录(为简洁起见,
我是一名优秀的程序员,十分优秀!