我有两个 nsmutablearray,一个是距离数组,另一个是包含链接到这些距离的图像的数组。我需要按升序对距离数组进行排序,然后基于此对图像数组进行排序。我试图以距离数组为键制作这两个数组的字典,但是当距离值相同时,在排序后的图像数组中返回相同的图像。谁能帮我解决这个问题。代码如下:
// 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]];