Objective c NSSortDescriptor订单值小于10.0

Objective c NSSortDescriptor订单值小于10.0,objective-c,Objective C,当距离小于10.0时,NSSortDescriptor排序不正确 代码(按距离排列): 正确排序(当距离值>10.0时): 顺序错误(当任何距离值小于10.0时): ( { 距离=“252.428821”; }, { 距离=“4514.256851”; } { distance=“8.362996”;//距离

当距离小于10.0时,
NSSortDescriptor
排序不正确

代码(按距离排列):

正确排序(当距离值>10.0时):

顺序错误(当任何距离值小于10.0时):

(
{
距离=“252.428821”;
},
{
距离=“4514.256851”;
}
{
distance=“8.362996”;//距离<10.0时顺序错误
},
)

我不认为这里的问题是该值小于10。看起来您的值是字符串,并且按字符顺序排列。“8”在“4”之后,所以要晚点

如果将字符串值转换为NSNumbers,它们应该正确排序

// Order Array by Distance (Distance = NSString)
NSSortDescriptor *aSortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"distance" ascending:YES];
[_contentData sortUsingDescriptors:[NSArray arrayWithObject:aSortDescriptor]];
    (
            {
            distance = "11.256649";
        },
            {
           distance = "257.428821";
        },
            {
            distance = "4534.256851";
        }
    )
    (
            {
           distance = "252.428821";
        },
            {
            distance = "4514.256851";
        }
            {
            distance = "8.362996"; // Wrong ordering when distance < 10.0
        },
    )