按下一级别值排序数组* - Ruby

时间:2021-09-16 16:00:19

I want to sort the films in this array by the popularity value. How can I do this?

我想通过流行度值对这个阵列中的电影进行排序。我怎样才能做到这一点?

[
  { 
    "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",
  }
]

2 个解决方案

#1


3  

my_array.sort_by { |e| e['popularity'] }

my_array.sort_by {| e | e ['人气']}

#2


0  

Obviously We should use Enumerable#sort_by here.

显然我们应该在这里使用Enumerable#sort_by。

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_by{|h| h["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"}]

Or I could also use Enumerable#sort :

或者我也可以使用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"}]

BENCHMARK

BENCHMARK

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)

#1


3  

my_array.sort_by { |e| e['popularity'] }

my_array.sort_by {| e | e ['人气']}

#2


0  

Obviously We should use Enumerable#sort_by here.

显然我们应该在这里使用Enumerable#sort_by。

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_by{|h| h["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"}]

Or I could also use Enumerable#sort :

或者我也可以使用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"}]

BENCHMARK

BENCHMARK

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)