美文网首页
对于数组中的某个对象进行排序

对于数组中的某个对象进行排序

作者: 41c48b8df394 | 来源:发表于2018-07-14 16:37 被阅读3次
    NSMutableArray *myMutableArr = [[[NSMutableArray alloc] init] autorelease];
    NSDictionary *dicOne = [NSDictionary dictionaryWithObjectsAndKeys:@"1",@"price",@"2",@"number", nil];
    NSDictionary *dicTWo = [NSDictionary dictionaryWithObjectsAndKeys:@"6",@"price",@"5",@"number", nil];
    NSDictionary *dicThree = [NSDictionary dictionaryWithObjectsAndKeys:@"3",@"price",@"1",@"number", nil];
    NSDictionary *dicFour = [NSDictionary dictionaryWithObjectsAndKeys:@"4",@"price",@"3",@"number", nil];
    [myMutableArr addObject:dicOne];
    [myMutableArr addObject:dicTWo];
    [myMutableArr addObject:dicThree];
    [myMutableArr addObject:dicFour];
    
    
    NSArray *sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"price" ascending:YES]];
    [myMutableArr sortUsingDescriptors:sortDescriptors];
    NSLog(@"排序后的数组%@",myMutableArr);
    
    
    - (NSArray *)compareWithArray:(NSArray *)array Key:(nullable NSString *)key{
        NSMutableArray *compareArray = [NSMutableArray arrayWithArray:array];
        NSSortDescriptor *sortDes = [NSSortDescriptor sortDescriptorWithKey:key ascending:false];//ascending:YES 代表升序 如果为NO 代表降序
        return [compareArray sortedArrayUsingDescriptors:@[sortDes]];
    }
    

    相关文章

      网友评论

          本文标题:对于数组中的某个对象进行排序

          本文链接:https://www.haomeiwen.com/subject/vjyypftx.html