[英]Sort array of strings based off of parent array?
我有几个月的正确顺序:
String[] monthOrder = {"January","Febuary","March","April","May","June","July","August","September","October","November","December"};
而且我有几个月的时间顺序不正确。 。 。
我该如何按不正确的顺序对月份数组进行排序,使其类似于正确的顺序。
编辑:
让我澄清一下,我正在从SQL Server读取表,它们最终将变成乱序,我可能甚至没有全部12个月的时间。 然后,我使用JFreeGraph从读取的表中绘制一个数字。X轴标签必须是我按正确顺序排列的月份数。 抱歉,一开始没有校准...
编辑2:代码:
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
m.setDatabase("Sioux Falls, SD");
int i = 0;
String newMonth = "";
String[] monthOrder = {"January","Febuary","March","April","May","June","July","August","September","October","November","December"};
Set<String> sqlMonths = new HashSet<String>();
while(i < m.getAmountOfDateTables()){
String date = m.getDateTables(i); //Gets the name of the table off the sql server FORMAT: Month Year
int index = date.indexOf(" "); //gets the index of the space to separate the month and year
String year = date.substring(index+1); //gets the year
int currentYearInt = Calendar.getInstance().get(Calendar.YEAR); //gets current year
String currentYear = String.valueOf(currentYearInt);//converts to string
System.out.println(year);//test
//I have a table called current need to ignore it.
if(date.equalsIgnoreCase("current")){
}else{
newMonth = date.substring(0,index); //gets the month
}
if(year.equalsIgnoreCase(currentYear) || year.equalsIgnoreCase("current")){ //making sure it doesnt graph anything from past or future years...
//dataset.addValue(m.getTotalForPersonInMonth(SalesPerson,m.getDateTables(i), Store),"Sales" , m.getDateTables(i)); THIS IS FOR GRAPHING WILL CHANGE WHEN SORTING IS DONE
if(date.equalsIgnoreCase("Current")){
}else{
sqlMonths.add(newMonth); //add to sql months...
}
i++;
//USED WHILE BECUASE FOR LOOP WOULDNT RUN WHILE WAS EASIER ANYWAY.
}else{
}
}
List<String> sortedMonths = new ArrayList<String>(); //your code
System.out.println(sqlMonths +" From sever"); //printing out sqlMonths
for (String month : monthOrder) {
if (sqlMonths.contains(month))
sortedMonths.add(month);
}
System.out.println(sortedMonths +" Sorted");
return dataset;
}
对不起,混乱:S
编辑3:哦,我我觉得很愚蠢……案子已经关闭了! :)非常感谢havexz!
这是您可以做的...
从SQL Server获得的作为HashSet
创建。 然后有这个逻辑。
String[] monthOrder = { "January", "Febuary", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December" };
Set<String> sqlMonths = new HashSet<String>();
// Write your code to load the sqlMonths.
sqlMonths.add("March");
sqlMonths.add("December");
sqlMonths.add("Febuary");
List<String> sortedMonths = new ArrayList<String>();
for (String month : monthOrder) {
if (sqlMonths.contains(month))
sortedMonths.add(month);
}
System.out.println(sortedMonths);
OUTPUT:
[Febuary, March, December]
希望这能解决您的问题。
注意:即使是这种情况,months的值也应与monthOrder
完全匹配。
一种方法是创建一个Map,在其中每个字符串都与其索引相对应: {"January" : 0, "February : 1, ...}
。然后编写一个Comparator,它接受两个字符串,并同时查找它们在地图中进行比较,然后比较生成的int。如何处理未出现在地图中的字符串是一个问题-如果您知道永远不会发生,则可能应该抛出异常,或者将所有排序列表的开头或结尾处的那些其他字符串(可能以确定性顺序,例如字母)。
在Java和其他语言中,您可以提供自己的比较器,例如在其中可以在正确的数组中查找字符串,并使用其索引位置作为数字值来确定顺序
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.