小编Jos*_*tus的帖子

根据另一个距离数组对nsmutablearray对象进行排序

我有两个nsmutablearrays,一个是距离数组,另一个是包含链接到这些距离的图像的数组.我需要按升序对距离数组进行排序,然后根据它对图像数组进行排序.我尝试用距离数组作为关键字来创建这两个数组的字典,但是当距离值相同时,在排序的图像数组中返回相同的图像.任何人都可以帮我解决这个问题.代码如下:

 // Put the two arrays into a dictionary as keys and values
    NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:_locationImages forKeys:_distances];
    // Sort the first array
    NSArray *sortedFirstArray = [[dictionary allKeys] sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        if ([obj1 floatValue] > [obj2 floatValue])
            return NSOrderedDescending;
        else if ([obj1 floatValue] < [obj2 floatValue])
            return NSOrderedAscending;
        return NSOrderedSame;
    }];
    // Sort the second array based on the sorted first array
    NSArray *sortedSecondArray = [dictionary objectsForKeys:sortedFirstArray notFoundMarker:[NSNull null]];           
Run Code Online (Sandbox Code Playgroud)

objective-c

3
推荐指数
1
解决办法
1471
查看次数

标签 统计

objective-c ×1