对月份(带字符串)排序算法
-
21-09-2019 - |
题
我有这个月的数组:
["January", "March", "December" , "October" ]
我想把它排序如下:
["January", "March", "October", "December" ]
我目前正在思考“如果/否则”可怕的级联,但我想知道是否还有其他方法可以做到这一点。
不好的部分是我只需要使用“字符串”来执行此操作(即不使用 Date 对象或类似的东西)
什么是好的方法?
解决方案
如果我有办法提供一个自定义排序顺序,我会创建定义正确的顺序列表:
correct = List("January", "February", "March", ...)
和然后通过在列表中的位置排序,是这样的:
toSort.sort(a, b) => compare(correct.index(a), correct.index(b))
其他提示
创建具有名称 - >索引的表,然后排序基于其在表中的值的数组。
一个几个例子可以是有用的,在C#arr.sort(myCompare)
,在Java Collections.sort(arr, myCompare)
,在Python arr.sort(myCompare)
,在PHP usort($arr, 'myCompare')
,在C ++ sort(vec.begin(), vec.end(), myCompare)
。
具有与正确排序的阵列,和排序基于它。
另一种解决方案(如果你的语言支持的话)是具有从月份名称为数字(1..12)的关联数组和使用阵列上排序执行自比较器。
解在Perl:d
my @mon = qw( January February March April May June July August September October November December );
my $mon;
@{$mon}{@mon} = (0..$#mon);
sub by_month {
$mon->{$a} <=> $mon->{$b};
}
sort by_month @data_to_sort
(虽然我敢肯定,一个球员能做到这一点在<30个字符)
和这里的在纯C溶液: HTTP://www.pnambic的.com / CPS / SortAnal / HTML / MonOrder.html
同事,
我看到的问题/业务问题持续超过2一年。 我决定写比较排序个月的名称(存储为字符串)正确。 这也适用于所需语言环境的月份名称 ==============比较=======================
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
/**
*
* @author akashtalyan
*/
public class MonthNamesComparator implements Comparator {
private static Map<Locale, List> monthMap = new HashMap<Locale, List>();
private final Locale locale;
public MonthNamesComparator(Locale locale) {
if (locale == null) {
throw new NullPointerException("MonthNamesComparator cannot accept null value for Locale parameter.");
}
List months = new ArrayList(12);
Calendar cal = Calendar.getInstance(locale);
SimpleDateFormat dateFormat = new SimpleDateFormat("MMMM", locale);
this.locale = locale;
if (!monthMap.containsKey(locale)) {
for (int i = 0; i < 12; i++) {
cal.set(Calendar.MONTH, i);
months.add(dateFormat.format(cal.getTime()).toLowerCase());
}
monthMap.put(locale , months);
}
}
@Override
public int compare(Object month1, Object month2) {
List months = monthMap.get(this.locale);
if (months == null) {
throw new NullPointerException("MonthNamesComparator cannot perform comparison - internal data is not initialized properly.");
}
return (months.indexOf(((String) month1).toLowerCase()) - months.indexOf(((String) month2).toLowerCase()));
}
}
和简单的测试类POC:
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
/**
*
* @author akashtalyan
*/
public class TestMonths {
public static void main(String[] args){
Locale en = Locale.ENGLISH, ru = new Locale("ru","RU");
String[] monthsToTestEn = new String[] {"FebRUary", "maY", "sepTember", "january", "december"};
String[] monthsToTestRu = new String[] {"АпреЛь", "январь", "Март", "Август"};
Map map = new TreeMap(new MonthNamesComparator(en));
int i = 0;
System.out.println("En Map original:");
for (String month : monthsToTestEn) {
System.out.print(month + " ");
map.put(month, new StringBuilder(String.valueOf(++i)).append(" position in source array").toString());
}
System.out.println();
System.out.println("En Map sorted:");
for (String month : (Set<String>)map.keySet()) {
System.out.println(month + " " + map.get(month));
}
i = 0;
map = new TreeMap(new MonthNamesComparator(ru));
System.out.println("Ru Map original:");
for (String month : monthsToTestRu) {
System.out.print(month + " ");
map.put(month, new StringBuilder(String.valueOf(++i)).append(" position in source array").toString());
}
System.out.println();
System.out.println("Ru Map sorted:");
for (String month : (Set<String>)map.keySet()) {
System.out.println(month + " " + map.get(month));
}
}
}
享受它,就像一个魅力。
创建映射:
month_map = {"January":1,
"February":2,
"March":3,
"April":4} # etc..
使用映射到一个月比较其他
或强>
大多数语言/框架有处理日期的对象。创建所有月份的日期对象,并将它们使用本机(如果可用)的不平等运营商或基本排序功能比较:
import datetime
January = datetime.date(2010,1,1)
February = datetime.date(2010,2,1)
if February < January: print("The world explodes.")
感谢所有的建议,我想作为接受来标记你的所有。
下面是结果代码:
// correct order
months as String[] = ["jan", "feb", "mar", "apr", "may", "jun",
"jul", "aug", "sep", "oct", "nov", "dec"]
// my unsorted months
myMonths as String[] = ["mar", "dec", "jul", "jan", "sep"]
// poor substitute for Map
mappedIndex as Int[]
// create an array with the corresponding index
for each m in myMonths do
i as Int = 0;
for each month in months do
if m == month then
mappedIndex[] = i // no break, so I should use "else"
else
i = i + 1
end
end
end
// At this point mapped index has the same order as my "unsorted" array
// in this case [2,11,5,0,8]
// Fortunately this language has "sort" otherwise I would jump out of the window
mappedIndex.sort()
// create a new array to hold the sorted values
myMonthsSorted as String[]
// and fill it with the correct value
for each i in mappedIndex do
myMonthsSorted[] = months[i]
end
对于像几个月,我只是硬编码,我需要的阵列...
var correctOrdering = {
english: ["January", "February", "March", ...],
french: ["Janvier", "Février", "Mars", ...],
russian: ["Январь", "февраль", "март"],
...
};
这不像月份名称将要很快改变。
太长了;博士
EnumSet.of( Month.JANUARY , Month.MARCH , Month.OCTOBER , Month.DECEMBER ).toString()
枚举
如果您的语言提供了强大的 枚举 像 Java 一样方便,定义了十几个对象。看 甲骨文教程.
java.time.Month
这 java.time 类包括方便的 Month
枚举,定义十几个对象,一个对象代表一年中一月到十二月的每个月。
它们编号为 1-12,并按正确的顺序定义,从 1 月到 12 月。
在您的代码库中,使用此枚举的对象来替换任何单纯整数的使用或月份名称字符串的使用。使用 Month
整个对象提供类型安全,确保有效值,并使您的代码更加自文档化。
在Java中, EnumSet
和 EnumMap
是的实现 Set
和 Map
针对枚举值进行了优化。它们执行速度非常快并且占用的内存很少。
EnumSet<Month> months = EnumSet.of( Month.JANUARY , Month.MARCH , Month.OCTOBER , Month.DECEMBER );
这 EnumSet
按自然顺序迭代,即声明枚举常量的顺序。所以 不需要显式排序 你的收藏。
该类包括一个 getDisplayName
生成月份名称的本地化字符串的方法。指定一个 TextStyle
您想要文本的长度或缩写。并指定一个 Locale
(a) 翻译中使用的人类语言,以及 (b) 决定缩写、标点符号和大写等问题的文化规范。
for( Month month : months ) {
String output = month.getDisplayName( TextStyle.SHORT_STANDALONE , Locale.CANADA_FRENCH ); // Or Locale.US, Locale.ITALY, etc.
System.out.println( output );
}
添加前缀每个月:
Jan -> aJan
Feb -> bFeb
...
排序,然后取出前缀。