根据父数组排序字符串数组?

时间:2011-12-07 01:24:09

标签: java arrays sorting jfreechart

我按照正确的顺序排列了几个月:

    String[] monthOrder = {"January","Febuary","March","April","May","June","July","August","September","October","November","December"};

我有一个月的数组不正确的顺序。 。

如何按照正确的顺序对数月的数组进行排序,而不是按照正确的顺序排列。

编辑:

让我澄清一下,我正在阅读SQL服务器的表格,他们最终将处于任何顺序,我甚至可能都没有全部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:哦,我感觉很愚蠢...案件已经关闭!! :)非常感谢你们!

3 个答案:

答案 0 :(得分:3)

这是你可以做的......

创建从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);

输出:

[Febuary, March, December]

希望这能解决你的问题。

注意:月份的值应与monthOrder

完全匹配,即使是这种情况

答案 1 :(得分:1)

一种方法是创建一个Map,其中每个字符串对应于其索引:{"January" : 0, "February : 1, ...}。然后你编写一个带有两个字符串的Comparator,在地图中查找它们,然后比较生成的int。你使用没有出现在地图中的字符串是一个问题 - 如果你知道它不应该发生,你应该抛出一个异常,或者把所有这些杂项字符串放在排序的开头或结尾列表(可能以某种确定的顺序,如字母顺序)。

答案 2 :(得分:1)

在java和其他语言中,您可以提供自己的比较器,例如,您可以在正确的数组中查找字符串,并使用其索引位置作为数值来确定顺序