我可以在nsuserdefaults中对存储的分数进行排序吗?

时间:2012-09-04 06:40:51

标签: iphone ios nsuserdefaults

我正在开发游戏应用程序,在那个应用程序中我存储名称,在nsuserdefaults中得分......但是 根据我的要求,我想根据分数存储名称和分数(从高到低)....是否有任何解决方案在nsuserdefaults中排序分数...我想在顶部显示我的高分

感谢先生

- (无效)btnSaveScore {       如果(!dictWinData)             dictWinData = [[NSMutableDictionary alloc] init];

    array = [[NSMutableArray alloc] init];
    array = [[[NSUserDefaults standardUserDefaults] valueForKey:@"ScoreName"] mutableCopy];
    if([array count] == 0)
    {
        array = [[NSMutableArray alloc] init];
    }

NSString *strName = [NSString stringWithFormat:@"%@",strNameOFPlayer];
NSString *strScore = [NSString stringWithFormat:@"%@",[NSString stringWithFormat:@"%d",iTap]];

if([strNameOFPlayer length]==7)
    [array addObject:[NSString stringWithFormat:@"%@                   %@",strName,strScore]];
else if ([strNameOFPlayer length] == 6)
    [array addObject:[NSString stringWithFormat:@"%@                     %@",strName,strScore]];
else if ([strNameOFPlayer length] == 5)
    [array addObject:[NSString stringWithFormat:@"%@                       %@",strName,strScore]];
else if ([strNameOFPlayer length] == 4)
    [array addObject:[NSString stringWithFormat:@"%@                         %@",strName,strScore]];
else if ([strNameOFPlayer length] == 3)
    [array addObject:[NSString stringWithFormat:@"%@                           %@",strName,strScore]];
else if ([strNameOFPlayer length] == 2)
    [array addObject:[NSString stringWithFormat:@"%@                             %@",strName,strScore]];
else if ([strNameOFPlayer length] == 1)
    [array addObject:[NSString stringWithFormat:@"%@                              %@",strName,strScore]];

   NSUserDefaults *dfltsData = [NSUserDefaults standardUserDefaults];
   [dfltsData setObject:array forKey:@"ScoreName"];
   [dfltsData synchronize];
   UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert"
                                                message:@"Score is saved."
                                               delegate:self
                                      cancelButtonTitle:@"Ok"
                                      otherButtonTitles:nil, nil];
          [alert show];
          [alert release];

请帮帮我

1 个答案:

答案 0 :(得分:1)

从NSUserDefault获取数组:

NSMutableArray *arrScores = [[NSUserDefaults standardUserDefaults] ObjectforKey:@"ScoreName"];

现在排序arrScores只应使用以下方法包含strScore值:

-(NSMutableArray *)sortByfloatvalue:(NSMutableArray *)array
{ 

  for(int i=0;i<[array count];i++)
  {
    for(int j=i+1;j<[array count];j++)
    {
        if([[array objectAtIndex:i] floatValue] < [[array objectAtIndex:j] floatValue])
        {
            [array exchangeObjectAtIndex:i withObjectAtIndex:j];
        }
    }
  }
  return array;
}