按对象属性对对象列表进行排序?

huangapple go评论75阅读模式
英文:

Sorting list of objects by object attribute?

问题

以下是您要翻译的代码部分:

I have a list of objects that are pulled in from an API. Here is the snippet of output (as there's about 300 lines):

     combo =>
     ID: 6, Name:Thomas Partey, Club:1, Position: 3, Price: $4.7, Total Pts: 57    
     ID: 7, Name:Martin Ødegaard, Club:1, Position: 3, Price: $7.0, Total Pts: 128    
     ID: 8, Name:Kieran Tierney, Club:1, Position: 2, Price: $4.6, Total Pts: 23    
     ID: 12, Name:Emile Smith Rowe, Club:1, Position: 3, Price: $5.6, Total Pts: 5 

I would like to change the order so that they are ranked by *Total Points* rather than *ID*

I have tried the following:

    sorted = combo.sort_by(@totalpoints)
As well as: (but I assume I want to try and use @teampoints since I've defined that)

    sorted = combo.sort_by(:totalpoints)

My Full code is:

    class Player
        attr_accessor :id, :firstname, :secondname, club, position, price, totalpoints, 
    :active
    def initialize(id, firstname, secondname, club, position, price, totalpoints, active)
        @id = id.to_i
        @firstname = firstname.to_s
        @secondname = secondname.to_s
        @club = club.to_s
        @position = position.to_i
        @price = price / 10.to_f
        @totalpoints = totalpoints.to_i
        @active = active.to_i
    end

    def to_s()
        "  ID: " + @id.to_s + ", Name:" + @firstname.to_s + " " + @secondname.to_s + ", Club:" + @club.to_s + ", Position: " + @position.to_s + ", Price: $" + @price.to_s + ", Total Pts: " + @totalpoints.to_s + "    "
    end

    def self.pull()
        require 'net/http'
        require 'json'
        url = 'https://fantasy.premierleague.com/api/bootstrap-static/'
        uri = URI(url)
        response = Net::HTTP.get(uri)
        object = JSON.parse(response)
        elements = object["elements"]
        elements.map! { |qb|
            if  qb["chance_of_playing_next_round"].to_f > 0
                Player.new(
                    qb["id"],                               # ID
                    qb["first_name"],                       # First Name
                    qb["second_name"],                      # Surname
                    qb["team"],                             # Club
                    qb["element_type"],                     # Position
                    qb["now_cost"],                         # Current Price
                    qb["total_points"],                     # Total Points
                    qb["chance_of_playing_next_round"])     # Chance Of Playing
            end
        }
    end


    combo = Player.pull().map{|qb| qb}
    sorted = combo.sort_by(@totalpoints)
    puts sorted

请注意,代码中包含HTML实体编码,这些需要在实际代码中进行处理,以便正确解析和运行。

英文:

I have a list of objects that are pulled in from an API. Here is the snippet of output (as there's about 300 lines):

 combo =>
ID: 6, Name:Thomas Partey, Club:1, Position: 3, Price: $4.7, Total Pts: 57    
ID: 7, Name:Martin Ødegaard, Club:1, Position: 3, Price: $7.0, Total Pts: 128    
ID: 8, Name:Kieran Tierney, Club:1, Position: 2, Price: $4.6, Total Pts: 23    
ID: 12, Name:Emile Smith Rowe, Club:1, Position: 3, Price: $5.6, Total Pts: 5 

I would like to change the order so that they are ranked by Total Points rather than ID

I have tried the following:

sorted = combo.sort_by(@totalpoints)

As well as: (but I assume I want to try and use @teampoints since I've defined that)

sorted = combo.sort_by(:totalpoints)

My Full code is:

class Player
attr_accessor :id, :firstname, :secondname, :club, :position, :price, :totalpoints, 
:active
def initialize(id, firstname, secondname, club, position, price, totalpoints, active)
@id = id.to_i
@firstname = firstname.to_s
@secondname = secondname.to_s
@club = club.to_s
@position = position.to_i
@price = price / 10.to_f
@totalpoints = totalpoints.to_i
@active = active.to_i
end
def to_s()
"  ID: " + @id.to_s + ", Name:" + @firstname.to_s + " " + @secondname.to_s + ", Club:" + @club.to_s + ", Position: " + @position.to_s + ", Price: $" + @price.to_s + ", Total Pts: " + @totalpoints.to_s + "    "
end
def self.pull()
require 'net/http'
require 'json'
url = 'https://fantasy.premierleague.com/api/bootstrap-static/'
uri = URI(url)
response = Net::HTTP.get(uri)
object = JSON.parse(response)
elements = object["elements"]
elements.map! { |qb|
if  qb["chance_of_playing_next_round"].to_f > 0
Player.new(
qb["id"],                               # ID
qb["first_name"],                       # First Name
qb["second_name"],                      # Surname
qb["team"],                             # Club
qb["element_type"],                     # Position
qb["now_cost"],                         # Current Price
qb["total_points"],                     # Total Points
qb["chance_of_playing_next_round"])     # Chance Of Playing
end
}
end
combo = Player.pull().map{|qb| qb}
sorted = combo.sort_by(@totalpoints)
puts sorted

end

答案1

得分: 1

根据您所展示的内容,这应该可以满足您的需求:

sorted = combo.sort_by(&:totalpoints)


实质上,这是上述代码的缩写版本:

sorted = combo.sort_by { |_combo| _combo.totalpoints }

英文:

Based on what you've got shown, this should do what you need:

sorted = combo.sort_by(&:totalpoints)

It's essentially a shortened version of this:

sorted = combo.sort_by { |_combo| _combo.totalpoints }

huangapple
  • 本文由 发表于 2023年2月18日 23:08:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75494238.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定