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

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

Sorting list of objects by object attribute?

问题

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

  1. 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):
  2. combo =>
  3. ID: 6, Name:Thomas Partey, Club:1, Position: 3, Price: $4.7, Total Pts: 57
  4. ID: 7, Name:Martin Ødegaard, Club:1, Position: 3, Price: $7.0, Total Pts: 128
  5. ID: 8, Name:Kieran Tierney, Club:1, Position: 2, Price: $4.6, Total Pts: 23
  6. ID: 12, Name:Emile Smith Rowe, Club:1, Position: 3, Price: $5.6, Total Pts: 5
  7. I would like to change the order so that they are ranked by *Total Points* rather than *ID*
  8. I have tried the following:
  9. sorted = combo.sort_by(@totalpoints)
  10. As well as: (but I assume I want to try and use @teampoints since I've defined that)
  11. sorted = combo.sort_by(:totalpoints)
  12. My Full code is:
  13. class Player
  14. attr_accessor :id, :firstname, :secondname, club, position, price, totalpoints,
  15. :active
  16. def initialize(id, firstname, secondname, club, position, price, totalpoints, active)
  17. @id = id.to_i
  18. @firstname = firstname.to_s
  19. @secondname = secondname.to_s
  20. @club = club.to_s
  21. @position = position.to_i
  22. @price = price / 10.to_f
  23. @totalpoints = totalpoints.to_i
  24. @active = active.to_i
  25. end
  26. def to_s()
  27. " 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 + " "
  28. end
  29. def self.pull()
  30. require 'net/http'
  31. require 'json'
  32. url = 'https://fantasy.premierleague.com/api/bootstrap-static/'
  33. uri = URI(url)
  34. response = Net::HTTP.get(uri)
  35. object = JSON.parse(response)
  36. elements = object["elements"]
  37. elements.map! { |qb|
  38. if qb["chance_of_playing_next_round"].to_f > 0
  39. Player.new(
  40. qb["id"], # ID
  41. qb["first_name"], # First Name
  42. qb["second_name"], # Surname
  43. qb["team"], # Club
  44. qb["element_type"], # Position
  45. qb["now_cost"], # Current Price
  46. qb["total_points"], # Total Points
  47. qb["chance_of_playing_next_round"]) # Chance Of Playing
  48. end
  49. }
  50. end
  51. combo = Player.pull().map{|qb| qb}
  52. sorted = combo.sort_by(@totalpoints)
  53. 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):

  1. combo =>
  2. ID: 6, Name:Thomas Partey, Club:1, Position: 3, Price: $4.7, Total Pts: 57
  3. ID: 7, Name:Martin Ødegaard, Club:1, Position: 3, Price: $7.0, Total Pts: 128
  4. ID: 8, Name:Kieran Tierney, Club:1, Position: 2, Price: $4.6, Total Pts: 23
  5. 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:

  1. sorted = combo.sort_by(@totalpoints)

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

  1. sorted = combo.sort_by(:totalpoints)

My Full code is:

  1. class Player
  2. attr_accessor :id, :firstname, :secondname, :club, :position, :price, :totalpoints,
  3. :active
  4. def initialize(id, firstname, secondname, club, position, price, totalpoints, active)
  5. @id = id.to_i
  6. @firstname = firstname.to_s
  7. @secondname = secondname.to_s
  8. @club = club.to_s
  9. @position = position.to_i
  10. @price = price / 10.to_f
  11. @totalpoints = totalpoints.to_i
  12. @active = active.to_i
  13. end
  14. def to_s()
  15. " 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 + " "
  16. end
  17. def self.pull()
  18. require 'net/http'
  19. require 'json'
  20. url = 'https://fantasy.premierleague.com/api/bootstrap-static/'
  21. uri = URI(url)
  22. response = Net::HTTP.get(uri)
  23. object = JSON.parse(response)
  24. elements = object["elements"]
  25. elements.map! { |qb|
  26. if qb["chance_of_playing_next_round"].to_f > 0
  27. Player.new(
  28. qb["id"], # ID
  29. qb["first_name"], # First Name
  30. qb["second_name"], # Surname
  31. qb["team"], # Club
  32. qb["element_type"], # Position
  33. qb["now_cost"], # Current Price
  34. qb["total_points"], # Total Points
  35. qb["chance_of_playing_next_round"]) # Chance Of Playing
  36. end
  37. }
  38. end
  39. combo = Player.pull().map{|qb| qb}
  40. sorted = combo.sort_by(@totalpoints)
  41. puts sorted

end

答案1

得分: 1

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

sorted = combo.sort_by(&:totalpoints)

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

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

英文:

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

  1. sorted = combo.sort_by(&:totalpoints)

It's essentially a shortened version of this:

  1. 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:

确定