按数字(最高的在前)然后按字母(按字母顺序)对对象集合进行排序

发布于 2024-08-21 00:40:01 字数 611 浏览 10 评论 0原文

我正在构建一个小部件来显示奥运会的奖牌数。我有一个“国家”对象的集合,其中每个对象都有一个“名称”属性,以及用于奖牌计数的“金”、“银”、“铜”。

列表应该排序: 1. 首先是奖牌总数 2.如果奖牌相同,则按类型细分(金>银>铜,即2金>1金+1银) 3.如果奖牌和类型相同,则按字母顺序进行子排序,

我正在用红宝石进行此操作,但我认为语言并不重要。我确实找到了一个解决方案,但如果感觉一定有一种更优雅的方法来做到这一点。

这就是我所做的:

  1. 使用加权奖牌总数创建虚拟属性。因此,如果他们有 2 枚金牌和 1 枚银牌,则加权总数将为“3.020100”。 1 枚金牌、1 枚银牌和 1 枚铜牌将为“3.010101”

  2. 由于我们希望将奖牌数排序为最高,因此列表按 DESC 排序。但之后我们想按字母顺序(即 ASC)进行子排序。因此,我创建了一个函数,可以对单词进行 alpha 反转(即“canada”=>“xzmzwz”)

  3. 将加权总计转换为字符串,连接反转的名称(即“3010101xzmzwz”),然后排序下降。瞧。

到目前为止,有人已经弄清楚如何用大约两行代码完成同样的事情。愿意启发我吗?

I'm building a widget to show medal counts for the Olympics. I have a collection of "country" objects, where each has a "name" attribute, and "gold", "silver", "bronze" for medal counts.

List should be sorted:
1. First by total medal count
2. If same medals, sub-sort by type (gold > silver > bronze, ie. two golds > 1 gold + 1 silver)
3. If same medals and type, sub-sort alphabetically

I'm doing this in ruby, but I suppose the language doesn't matter. I did figure out a solution, but if feels like there must be a much more elegant way to do it.

Here's what I did:

  1. Create a virtual attribute with the weighted medal total. So if they had 2 gold and 1 silver, weighted total would be "3.020100". 1 gold and 1 silver and 1 bronze would be "3.010101"

  2. Since we want to sort the medal count as highest first, list is sorted DESC. But then we want to sub-sort alphabetically (ie. ASC) after that. So I created a function which would alpha-invert a word (ie. "canada" => "xzmzwz")

  3. Convert the weighted total to a string, concat the reversed name (ie. "3010101xzmzwz"), then sort descending. Voila.

By now, someone has figured out how to do the same thing in about 2 lines of code. Care to enlighten me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌酱 2024-08-28 00:40:01
countries.sort_by do |country|
  medals = country.gold + country.silver + country.bronze
  [-medals, -country.gold, -country.silver, country.name]
end
countries.sort_by do |country|
  medals = country.gold + country.silver + country.bronze
  [-medals, -country.gold, -country.silver, country.name]
end
云巢 2024-08-28 00:40:01

一个简单的方法是使用 sort_by 和一些任意格式的字符串,例如:

countries.sort_by do |c|
  "%010d-%010d-%010d-%s" % [ c.gold, c.silver, c.bronze, c.name ]
end

通过将赢得的奖牌数填充到可能令人震惊的 10 位,将所有国家/地区转换为 ASCII 可排序列表。如果有人赢得了超过 100 亿枚奖牌,你的程序可能会出现故障,但这似乎是一个合理的限制。

A simple method is to use sort_by with some arbitrary formatted string, like:

countries.sort_by do |c|
  "%010d-%010d-%010d-%s" % [ c.gold, c.silver, c.bronze, c.name ]
end

This converts all the countries in to an ASCII sortable listing by padding the number of medals won to the presumably outrageous 10 places. If anyone wins more than ten billion medals your program may malfunction, but that seems like a reasonable constraint.

飞烟轻若梦 2024-08-28 00:40:01

在Java中,您可以在对象上实现Comparable,然后可以轻松地在ArrayList或Array中对其进行排序。 Ruby 中是否有一种机制可以告诉您如何比较两个“Country”对象?

In Java you would implement comparable on your object and then it could easily be sorted in an ArrayList or Array. Is there a mechanism in Ruby to tell how to compare two of your "Country" objects?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文