Home > Enterprise >  How to get index of an element on sorted hash
How to get index of an element on sorted hash

Time:07-25

on my website i would like display the ranking of the top subscribers.

Here is my controller:

@users_top = User.all.sort_by{|e| e[:total_days_sub]}.reverse.first(10)
@users_sort = User.all.sort_by{|e| e[:total_days_sub]}.reverse

for the top 10 I have finished, the @users_top is ok.

But how can I have the index of an element of my hash @users_sort in depending of the current_user.email ?

CodePudding user response:

I just have to do

@users_sort = User.all.sort_by{|e| e[:total_days_sub]}.reverse.pluck(:email).find_index(current_user.email)
  • Related