我使用排序描述符按升序对距离数组进行排序,但是有几个索引没有保存距离,所以我在找不到距离的地方存储了更多的数字,这有助于我正确地对数组进行排序。但是在找不到距离的地方存储更多的数字并不是一个好的方法。因此,我正在寻找一种解决方案,将距离按升序排列,在哪里找到,在哪里找不到,这样它就应该在结束索引中排序,而不需要在NSArray中硬编码更大的数字。我的排序代码如下所示。
NSLog(@"%@", self.distanceArr); // self.distanceArr is a Mutable Array
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"OnlyDistance" ascending:YES];
NSArray *sort = [self.distanceArr mutableCopy];
NSArray *sortedArray = [sort sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
NSLog(@"Sorted %@",sortedArray);
NSLog(@"%d",[sortedArray count]);
self.sortedBusinessDetArr = [sortedArray mutableCopy];
[self.searchTbl reloadData];
我的数组如下所示
(
{
OnlyDistance = "10000.0"; /// Hard Coded Value where distance is not found.
},
{
OnlyDistance = "13089.53";
},
{
OnlyDistance = "12991.81";
},
{
OnlyDistance = "10000.0"; /// Hard Coded Value where distance is not found.
},
{
OnlyDistance = "10000.0"; /// Hard Coded Value where distance is not found.
},
{
OnlyDistance = "13089.53";
}
)
OnlyDistance是存储浮点值的NSNumber。如果我存储的0.0是NSNumber,那么它也是以升序排序的,顶部是零。并且我希望保存零的值或未找到的结果被排序为底部,只有保存值的索引应该位于顶部。我不能按降序排序。
转载请注明出处:http://www.mlhpz.com/article/20230520/2206082.html