NSDictionary 按浮点数形式对键进行排序

发布于 2024-09-24 08:01:14 字数 452 浏览 0 评论 0原文

基本上我有一个带有键和值的 NSDictionary。

键都是数字,但目前它们是字符串。

我希望能够将它们作为数字进行比较以便对它们进行排序。

例如:如果我有一个这样的字典:

{
  "100"  => (id)object,
  "20"   => (id)object,
  "10"   => (id)object,
  "1000" => (id)object,
}

我希望能够像这样排序:

{
  "10"   => (id)object,
  "20"   => (id)object,
  "100"  => (id)object,
  "1000" => (id)object,
}

有什么想法吗?

谢谢

汤姆

basically I have an NSDictionary with keys and values.

The keys are all numbers, but at the moment they are strings.

I want to be able to compare them as numbers in order to sort them.

eg: If I have a Dictionary like this:

{
  "100"  => (id)object,
  "20"   => (id)object,
  "10"   => (id)object,
  "1000" => (id)object,
}

I want to be able to sort it like this:

{
  "10"   => (id)object,
  "20"   => (id)object,
  "100"  => (id)object,
  "1000" => (id)object,
}

Any ideas?

Thanks

Tom

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

乱世争霸 2024-10-01 08:01:14

不确定你在做什么——字典本质上是未排序的,默认实现中没有稳定的键排序。如果你想通过排序键遍历值,你可以这样做:

NSInteger floatSort(id num1, id num2, void *context)
{
    float v1 = [num1 floatValue];
    float v2 = [num2 floatValue];
    if (v1 < v2)
        return NSOrderedAscending;
    else if (v1 > v2)
        return NSOrderedDescending;
    else
        return NSOrderedSame;
}

NSArray *allKeys = [aDictionary allKeys];
NSArray *sortedKeys = [allKeys sortedArrayUsingFunction:floatSort context:NULL];
for (id key in sortedKeys)
    id val = [aDictionary objectForKey:key];
    …

Not sure what you are up to – dictionaries are inherently unsorted, there is no stable key ordering in the default implementation. If you want to walk the values by sorted keys, you can do something like this:

NSInteger floatSort(id num1, id num2, void *context)
{
    float v1 = [num1 floatValue];
    float v2 = [num2 floatValue];
    if (v1 < v2)
        return NSOrderedAscending;
    else if (v1 > v2)
        return NSOrderedDescending;
    else
        return NSOrderedSame;
}

NSArray *allKeys = [aDictionary allKeys];
NSArray *sortedKeys = [allKeys sortedArrayUsingFunction:floatSort context:NULL];
for (id key in sortedKeys)
    id val = [aDictionary objectForKey:key];
    …
坐在坟头思考人生 2024-10-01 08:01:14

您无法对字典进行排序,但可以将键作为数组获取,对其进行排序,然后按该顺序输出。 sortedArrayUsingComparator 会执行此操作,您可以将字符串与 NSNumericSearch 选项进行比较。

NSArray* keys = [myDict allKeys];
NSArray* sortedArray = [keys sortedArrayUsingComparator:^(id a, id b) { 
    return [a compare:b options:NSNumericSearch]; 
}]; 

for( NSString* aStr in sortedArray ) {
    NSLog( @"%@ has key %@", [myDict objectForKey:aStr], aStr );
}

You can't sort a dictionary, but you can get the keys as an array, sort that, then output in that order. sortedArrayUsingComparator will do that, and you can compare the strings with the NSNumericSearch option.

NSArray* keys = [myDict allKeys];
NSArray* sortedArray = [keys sortedArrayUsingComparator:^(id a, id b) { 
    return [a compare:b options:NSNumericSearch]; 
}]; 

for( NSString* aStr in sortedArray ) {
    NSLog( @"%@ has key %@", [myDict objectForKey:aStr], aStr );
}
听风念你 2024-10-01 08:01:14

compare:options:NSNumericSearch 结合使用。

Use compare:options: with NSNumericSearch.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文