按下一级值对数组顶层排序-Ruby

按下一级值对数组顶层排序-Ruby,ruby,arrays,Ruby,Arrays,我想按受欢迎程度对这个数组中的电影进行排序。我该怎么做 [ { "id"=>1, "popularity"=>7.77030797174916, "title"=>"Another film", }, { "id"=>2, "popularity"=>2.7703074916, "title"=>"A film", }, { "id"=>3, "p

我想按受欢迎程度对这个数组中的电影进行排序。我该怎么做

[
  { 
    "id"=>1,  
    "popularity"=>7.77030797174916, 
    "title"=>"Another film",
  }, 
  { 
    "id"=>2,  
    "popularity"=>2.7703074916, 
    "title"=>"A film",
  },
  { 
    "id"=>3,  
    "popularity"=>9.77030797174916, 
    "title"=>"A third film",
  }
]
显然我们应该在这里使用

或者我也可以使用
Enumerable#sort

a = [
  { 
    "id"=>1,  
    "popularity"=>7.77030797174916, 
    "title"=>"Another film",
  }, 
  { 
    "id"=>2,  
    "popularity"=>2.7703074916, 
    "title"=>"A film",
  },
  { 
    "id"=>3,  
    "popularity"=>9.77030797174916, 
    "title"=>"A third film",
  }
]
a.sort{|h1,h2| h1["popularity"] <=> h2["popularity"]}
# => [{"id"=>2, "popularity"=>2.7703074916, "title"=>"A film"},
#     {"id"=>1, "popularity"=>7.77030797174916, "title"=>"Another film"},
#     {"id"=>3, "popularity"=>9.77030797174916, "title"=>"A third film"}]
a=[
{ 
“id”=>1,
“受欢迎程度”=>7.77030797174916,
“标题”=>“另一部电影”,
}, 
{ 
“id”=>2,
“受欢迎程度”=>2.7703074916,
“片名”=>“一部电影”,
},
{ 
“id”=>3,
“受欢迎程度”=>9.77030797174916,
“标题”=>“第三部电影”,
}
]
a、 排序{| h1,h2 | h1[“流行”]h2[“流行”]}
#=>[{“id”=>2,“人气”=>2.7703074916,“片名”=>“一部电影”},
#{“id”=>1,“人气”=>7.77030797174916,“片名”=>“另一部电影”},
#{“id”=>3,“人气”=>9.77030797174916,“标题”=>“第三部电影”}]
基准

require 'benchmark'

a = [
  { 
    "id"=>1,  
    "popularity"=>7.77030797174916, 
    "title"=>"Another film",
  }, 
  { 
    "id"=>2,  
    "popularity"=>2.7703074916, 
    "title"=>"A film",
  },
  { 
    "id"=>3,  
    "popularity"=>9.77030797174916, 
    "title"=>"A third film",
  }
]

Benchmark.bm(100) do |b|
  b.report("Sort")    { a.sort{|h1,h2| h1["popularity"] <=> h2["popularity"]} }
  b.report("Sort by") { a.sort_by{|h| h["popularity"]} }
end

                 user     system      total        real
Sort         0.000000   0.000000   0.000000 (  0.000041)
Sort by      0.000000   0.000000   0.000000 (  0.000019)
需要“基准测试”
a=[
{ 
“id”=>1,
“受欢迎程度”=>7.77030797174916,
“标题”=>“另一部电影”,
}, 
{ 
“id”=>2,
“受欢迎程度”=>2.7703074916,
“片名”=>“一部电影”,
},
{ 
“id”=>3,
“受欢迎程度”=>9.77030797174916,
“标题”=>“第三部电影”,
}
]
基准bm(100)do|b|
b、 报告(“排序”){a.Sort{| h1,h2 | h1[“流行”]h2[“流行”]}
b、 报告(“排序依据”){a.Sort_by{h | h[“受欢迎程度”]}
结束
用户系统总真实值
排序0.0000000.0000000.000000(0.000041)
按0.0000000.0000000.000000(0.000019)排序
显然我们应该在这里使用

或者我也可以使用
Enumerable#sort

a = [
  { 
    "id"=>1,  
    "popularity"=>7.77030797174916, 
    "title"=>"Another film",
  }, 
  { 
    "id"=>2,  
    "popularity"=>2.7703074916, 
    "title"=>"A film",
  },
  { 
    "id"=>3,  
    "popularity"=>9.77030797174916, 
    "title"=>"A third film",
  }
]
a.sort{|h1,h2| h1["popularity"] <=> h2["popularity"]}
# => [{"id"=>2, "popularity"=>2.7703074916, "title"=>"A film"},
#     {"id"=>1, "popularity"=>7.77030797174916, "title"=>"Another film"},
#     {"id"=>3, "popularity"=>9.77030797174916, "title"=>"A third film"}]
a=[
{ 
“id”=>1,
“受欢迎程度”=>7.77030797174916,
“标题”=>“另一部电影”,
}, 
{ 
“id”=>2,
“受欢迎程度”=>2.7703074916,
“片名”=>“一部电影”,
},
{ 
“id”=>3,
“受欢迎程度”=>9.77030797174916,
“标题”=>“第三部电影”,
}
]
a、 排序{| h1,h2 | h1[“流行”]h2[“流行”]}
#=>[{“id”=>2,“人气”=>2.7703074916,“片名”=>“一部电影”},
#{“id”=>1,“人气”=>7.77030797174916,“片名”=>“另一部电影”},
#{“id”=>3,“人气”=>9.77030797174916,“标题”=>“第三部电影”}]
基准

require 'benchmark'

a = [
  { 
    "id"=>1,  
    "popularity"=>7.77030797174916, 
    "title"=>"Another film",
  }, 
  { 
    "id"=>2,  
    "popularity"=>2.7703074916, 
    "title"=>"A film",
  },
  { 
    "id"=>3,  
    "popularity"=>9.77030797174916, 
    "title"=>"A third film",
  }
]

Benchmark.bm(100) do |b|
  b.report("Sort")    { a.sort{|h1,h2| h1["popularity"] <=> h2["popularity"]} }
  b.report("Sort by") { a.sort_by{|h| h["popularity"]} }
end

                 user     system      total        real
Sort         0.000000   0.000000   0.000000 (  0.000041)
Sort by      0.000000   0.000000   0.000000 (  0.000019)
需要“基准测试”
a=[
{ 
“id”=>1,
“受欢迎程度”=>7.77030797174916,
“标题”=>“另一部电影”,
}, 
{ 
“id”=>2,
“受欢迎程度”=>2.7703074916,
“片名”=>“一部电影”,
},
{ 
“id”=>3,
“受欢迎程度”=>9.77030797174916,
“标题”=>“第三部电影”,
}
]
基准bm(100)do|b|
b、 报告(“排序”){a.Sort{| h1,h2 | h1[“流行”]h2[“流行”]}
b、 报告(“排序依据”){a.Sort_by{h | h[“受欢迎程度”]}
结束
用户系统总真实值
排序0.0000000.0000000.000000(0.000041)
按0.0000000.0000000.000000(0.000019)排序

my_array.sort_by{e|e['popularity']}
my_array.sort_by{e|e['popularity']}
使用
sort_by
然后编写一个提取popularity的块
sort_by
,然后编写一个提取popularity的块