Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/43.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在iPhone中按降序排列数组_Ios_Iphone_Objective C_Nsarray_Nssortdescriptor - Fatal编程技术网

Ios 如何在iPhone中按降序排列数组

Ios 如何在iPhone中按降序排列数组,ios,iphone,objective-c,nsarray,nssortdescriptor,Ios,Iphone,Objective C,Nsarray,Nssortdescriptor,我正在尝试按降序对数组排序,我可以按升序对数组排序,这是我按升序排序的代码 NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"year" ascending:NO]; NSArray * descriptors = [NSArray arrayWithObjects:sortDescriptor, nil]; sortedArray = [array sortedArrayUsingDescrip

我正在尝试按降序对数组排序,我可以按升序对数组排序,这是我按升序排序的代码

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"year" ascending:NO];
NSArray * descriptors = [NSArray arrayWithObjects:sortDescriptor, nil];
sortedArray = [array sortedArrayUsingDescriptors:descriptors];

dictionaryWithSortedYears = [[NSMutableDictionary alloc] init];
NSString *tempDateKey = nil;

for (TreeXmlDetails *list in sortedArray)
{
    id obj = [dictionaryWithSortedYears objectForKey:list.year];

    if(!obj)
    {
        if(tempDateKey == nil)
        {
            arrayFromList = [[NSMutableArray alloc] init];
            tempDateKey = list.year;
        }
    }

    if([list.year isEqualToString:tempDateKey])
        [arrayFromList addObject:list];
    else
    {
        [dictionaryWithSortedYears setObject:arrayFromList forKey:tempDateKey];
        tempDateKey = nil;
        arrayFromList = nil;
        arrayFromList = [[NSMutableArray alloc] init];
        tempDateKey = list.year;
        [arrayFromList addObject:list];
    }
}

[dictionaryWithSortedYears setObject:arrayFromList forKey:tempDateKey];

NSArray *arr = [dictionaryWithSortedYears allKeys];

sortedKeys = [[NSArray alloc] initWithArray:[arr sortedArrayUsingSelector:@selector(compare:)]];
但是,我想在降序数组中对数组进行排序,请帮助我。 感谢您的建议。

请使用以下内容:

[yourArrayToBeSorted sortUsingComparator:^NSComparisonResult(id obj1, id obj2){
    return [(NSDate *)obj2 compare:(NSDate *)obj1];} ];
从文档中:

NSSortDescriptor的一个实例描述了对对象进行排序的基础 通过指定用于比较对象的属性,方法 若要用于比较属性,以及是否应进行比较 上升或下降。NSSortDescriptor的实例是不可变的

您可以通过指定密钥来构造NSSortDescriptor的实例 要比较的属性路径,排序顺序(升序) 或降序),以及(可选)用于执行 比较

建造商: initWithKey:升序:

返回使用给定属性初始化的NSSortDescriptor对象 键路径和排序顺序,并使用默认的比较选择器。 -(id)initWithKey:(NSString*)键路径升序:(BOOL)升序

指定顺序的参数:

升序是如果接收方指定按升序排序, 否则不行

这些apple文档将逐步指导您如何对阵列进行排序:


您必须具有NSMutableArray类型的“yourArrayToBeSorted”。