【发布时间】:2013-01-11 22:51:29
【问题描述】:
在 StefanB 在How to sort NSMutableArray using sortedArrayUsingDescriptors? 中的回答的帮助下,我得到了很好的理解,因此在我的项目中实现了 NSSortDescriptors,我有来自 Facebook Graph 的 Facebook Places 名称、描述、id、long 和 lat。然后使用我的位置和来自 Facebook Places 的经纬度计算距离。并将所有这些保存在字典的数组(placesDataArray)中。
现在的问题是当尝试对这个数组(placesDataArray)进行排序时,返回数组(sortedArray)为空?
NSLog(@"PLACES DATA ARRAY ===== > %@", placesDataArray);
NSSortDescriptor * distanceDescriptor = [[NSSortDescriptor alloc] initWithKey:DISTANCE
ascending:YES];
id obj;
NSEnumerator * enumerator = [placesDataArray objectEnumerator];
while ((obj = [enumerator nextObject]));
NSArray * descriptors =
[NSArray arrayWithObjects:distanceDescriptor, nil];
NSArray * sortedArray =
[placesDataArray sortedArrayUsingDescriptors:descriptors];
NSLog(@"\nSorted =========================>");
enumerator = [sortedArray objectEnumerator];
while ((obj = [enumerator nextObject]));
NSLog(@"SORTED ARRAY ===========> \n%@", obj);
我的结果是:
> 2013-01-10 18:50:40.439 Chat.Points[12091:c07] PLACES DATA ARRAY ===== > (
{
category = Hotel;
distance = "0.109";
name = "New York Marriott Marquis";
placeImageString = 20372413613;
},
{
category = Hotel;
distance = "0.019";
name = "DoubleTree Suites by Hilton New York City - Times Square";
placeImageString = 85286252698;
},
.
.
.
.
.
{
category = "Local business";
distance = "0.229";
name = "Le Pain Quotidien";
placeImageString = 153110388035573;
},
{
category = "Local business";
distance = "0.074";
name = NYSC;
placeImageString = 144712562228653;
},
{
category = "Local business";
distance = "0.193";
name = "The W Hotel";
placeImageString = 113613138695956;
},
{
category = "Local business";
distance = "0.015";
name = "Palace Theatre - Pricilla Queen Of The Desert!";
placeImageString = 130723233698153;
},
{
category = Hotel;
distance = "0.033";
name = "Renaissance New York Times Square Hotel";
placeImageString = 111789988858447;
} )
2013-01-10 18:50:40.500 Chat.Points[12091:c07] Sorted =========================>
2013-01-10 18:50:44.993 Chat.Points[12091:c07] SORTED ARRAY ===========>
(null)
请参考我上面提供的链接中的问题。 非常感谢您的努力。
【问题讨论】:
-
这里的
DISTANCE是什么? -
感谢 Midhun MP,'DISTANCE' 是保存 'distance' 对象值的关键,正如您在我的代码中所见,例如 - distance = "0.229"
标签: iphone sorting nssortdescriptor