【发布时间】:2023-03-28 23:35:02
【问题描述】:
我有一个这种格式的哈希:
images_found = {
"results" => [
{"img_1"=>1.923076923076923},
{"img_2"=>0.8241758241758242},
{"img_3"=>0.6868131868131868},
{"img_4"=>0.5494505494505495},
{"img_5"=>0.4120879120879121},
{"img_6"=>0.3434065934065934},
{"img_7"=>0.2747252747252747},
{"img_8"=>0.2060439560439561},
{"img_9"=>0.1373626373626374},
{"img_10"=>0.06868131868131869},
{"img_11"=>0.5494505494505495},
{"img_12"=>0.3434065934065934},
{"img_13"=>0.2747252747252747},
{"img_14"=>0.2060439560439561},
{"img_15"=>0.1373626373626374},
{"img_16"=>0.06868131868131869}
],
"type" => "SEARCH_RESULTS"
}
我想按值从高到低对images_found["results"] 进行排序,结果将是:
images_found = {
"results"=> [
{"img_1"=>1.923076923076923},
{"img_2"=>0.8241758241758242},
{"img_3"=>0.6868131868131868},
{"img_4"=>0.5494505494505495},
{"img_11"=>0.5494505494505495},
{"img_5"=>0.4120879120879121},
{"img_6"=>0.3434065934065934},
{"img_12"=>0.3434065934065934},
{"img_7"=>0.2747252747252747},
{"img_13"=>0.2747252747252747},
{"img_8"=>0.2060439560439561},
{"img_14"=>0.2060439560439561},
{"img_9"=>0.1373626373626374},
{"img_15"=>0.1373626373626374},
{"img_10"=>0.06868131868131869},
{"img_16"=>0.06868131868131869}
],
"type" => "SEARCH_RESULTS"
}
我尝试的解决方案是images_found["results"].sort_by! {|_key, value| value},但它不起作用。
当我尝试调试 value 时,它正在打印 nil
我四处寻找解决方案,但没有找到与此问题陈述相匹配的任何内容。
请推荐!
【问题讨论】: