对月份(带字符串)排序算法
我有这个月的数组:
["January", "March", "December" , "October" ]
我想将其排序如下:
["January", "March", "October", "December" ]
我目前正在思考“if/else”可怕的级联,但我想知道是否还有其他方法可以做到这一点。
不好的部分是我只需要使用“字符串”来执行此操作(即不使用 Date 对象或类似的东西)
什么是一个好的方法?
I have this months array:
["January", "March", "December" , "October" ]
And I want to have it sorted like this:
["January", "March", "October", "December" ]
I'm currently thinking in a "if/else" horrible cascade but I wonder if there is some other way to do this.
The bad part is that I need to do this only with "string" ( that is, without using Date object or anything like that )
What would be a good approach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
如果我有办法提供自定义排序顺序,我会创建一个定义正确顺序的列表:
然后按该列表中的位置排序,如下所示:
If I had a way to supply a custom sorting order, I'd create a list defining the correct order:
And then sort by the position in that list, something like:
创建一个包含 name->index 的表,然后根据数组在表中的值对数组进行排序。
几个示例可能有用,在 C# 中
arr.sort(myCompare)
中,在 Java 中Collections.sort(arr, myCompare)
中,在 Python 中arr.sort (myCompare)
,在 PHP 中usort($arr, 'myCompare')
,在 C++ 中sort(vec.begin(), vec.end(), myCompare)
代码>.Create a table with name->index, then sort the array based on its value in the table.
A couple of examples may be useful, in C#
arr.sort(myCompare)
, in JavaCollections.sort(arr, myCompare)
, in Pythonarr.sort(myCompare)
, in PHPusort($arr, 'myCompare')
, in C++sort(vec.begin(), vec.end(), myCompare)
.拥有一个具有正确排序的数组,并基于它进行排序。
另一个解决方案(如果您的语言支持)是拥有一个从月份名称到数字 (1..12) 的关联数组,并使用自定义比较器在数组上运行排序。
Perl 中的解决方案:D
(尽管我确信高尔夫球手可以在 < 30 个字符内做到这一点)
,这是纯 C 中的解决方案: http://www.pnambic.com/CPS/SortAnal/html/MonOrder.html
Have an array with the proper sort, and sort based on it.
Another solution (if your language supports it) is to have an associative array from month names to numbers (1..12) and use a custom comparator running sort on your array.
Solution in Perl :D
(although I'm sure a golfer could do that in < 30 characters)
And here's a solution in plain C : http://www.pnambic.com/CPS/SortAnal/html/MonOrder.html
从 Java POV 的角度来说,我要拉皮条(就像我经常做的那样)google-collections (很快将被 Guava 取代):
...以及你完成了。
如果其他人已经这样做了,那么就不要自己编写它,这可能比您可能更有效并且使用更好的 API。
在一般情况下没有帮助,但以防万一任何 Java 人员遇到同样的问题......
Talking from a Java POV, I'm going to pimp (as I often do) google-collections (soon to be replaced with Guava):
... and you're done.
Don't write it yourself if someone else has done it, probably more efficiently and with a nicer API than you probably would.
Not helpful in the general case, but just in case any Java folk have the same problem...
同事们,
我看到这个问题/业务问题持续了两年多。
我决定编写比较器来正确排序月份的名称(存储为字符串)。
它还包含所需区域设置的月份名称
============== 比较器 =========================
POC 的简单测试类:
享受它,就像一种魅力。
Colleagues,
I see the issue/business problem lasts more than 2 year.
I decided to write comparator for sorting months' names (stored as strings) properly.
It also holds names of the months for desired locale
============== Comparator =======================
And simple test class to POC:
Enjoy it, works like a charm.
创建映射:
使用映射将一个月与另一个月进行比较。
或
大多数语言/框架都有用于处理日期的对象。创建所有月份的日期对象,并使用本机(如果可用)不等运算符或基本排序函数对它们进行比较:
Create a mapping:
Use the mapping to compare one month to the other.
OR
Most languages/frameworks have objects for handling dates. Create date objects for all the months and compare them using the native (if available) inequality operators or basic sorting functions:
感谢大家的建议,我想将你们全部标记为已接受。
这是生成的代码:
Thanks all for the suggestions, I would like to mark you all as accepted.
Here's the resulting code:
对于像月份这样的东西,我只是硬编码我需要的数组......
月份名称不会很快改变。
For something like months, I'd just hard-code the arrays I needed...
It's not like month names are going to change any time soon.
tl;dr
Enum
如果您的语言提供了强大的 enum 像 Java 一样方便,定义十几个对象。请参阅 Oracle 教程。
java.time.Month
java.time 类包括方便的
Month
枚举,定义十几个对象,每个对象代表一年中的一月到十二月。它们编号为 1-12,并按正确的顺序定义,从 1 月到 12 月。
在您的代码库中,使用此枚举的对象来替换任何单纯整数的使用或月份名称字符串的使用。在整个过程中使用
Month
对象可以提供类型安全,确保值有效,并使您的代码更加自文档化。在 Java 中,
EnumSet
< /a> 和EnumMap
< /a> 是Set
和地图
针对枚举值进行了优化。它们执行速度非常快并且占用的内存很少。EnumSet
按自然顺序(即声明枚举常量的顺序)进行迭代。因此无需显式排序您的集合。该类包括
getDisplayName
方法,用于生成月份名称的本地化字符串。指定TextStyle
< /a> 您想要文本的长度或缩写。并为 (a) 翻译中使用的人类语言和 (b) 决定缩写、标点符号和大写等问题的文化规范指定一个Locale
。tl;dr
Enum
If your language provides a powerful enum facility as does Java, define a dozen objects. See Oracle Tutorial.
java.time.Month
The java.time classes include the handy
Month
enum, defining a dozen objects one for each month of the year January-December.They are numbered 1-12, and defined in proper order, January to December.
In your code base, use objects of this enum to replace any use of mere integers or use of name-of-month strings. Using
Month
objects throughout provides type-safety, ensures valid values, and makes your code more self-documenting.In Java, the
EnumSet
andEnumMap
are implementations ofSet
andMap
that are optimized for enum values. They execute very quickly and take very little memory.The
EnumSet
iterates in natural order, the order in which the enum constants are declared. So no need to explicitly sort your collection.The class includes a
getDisplayName
method for generating a localized String of the name of the month. Specify aTextStyle
for how long or abbreviated you want the text. And specify aLocale
for (a) the human language to use in translation, and (b) the cultural norms to decide issues such as abbreviation, punctuation, and capitalization.为每个月添加前缀:
排序,然后删除前缀。
Add a prefix for each month:
Sort, then remove the prefix.