如何排序这个列表?

时间:2010-10-21 12:20:41

标签: java sorting

我有一个字符串列表

ArrayList<String> list = new ArrayList<String>();

每个项目:

list.add(week+" "+year);

其中weekyear是整数。

如何将此列表按升序排序?

10 个答案:

答案 0 :(得分:10)

我建议创建一个包含所需数据并实现可比数据的数据结构。

class WeekOfYear implements Comparable<WeekOfYear> {
    private int week ;
    private int year ;
    public WeekOfYear(int week, int year) {
        this.week = week ;
        this.year = year ;
    }
    /**
     * ...
     */
    public int compareTo(WeekOfYear other) {
        int delta = this.year - other.year ;

        return (int)Math.signum(
                delta != 0 ? delta : (this.week - other.week));
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder() ;
        builder.append(week > 9 ? "" : "0") ;
        builder.append(week).append(" ") ;
        builder.append(year) ;

        return builder.toString();
    }
}

List<WeekOfYear> weeks ;
weeks = new ArrayList<WeekOfYear>();
Collections.sort(weeks) ;

答案 1 :(得分:5)

您需要编写一个比较器,该比较器分割周和年,比较前一周(如果年份字段相等)。 顺便说一下你应该把一周和一年放在一个物体中以避免分裂。

答案 2 :(得分:3)

Collections.sort(list, new Comparator<String>() {
  public int compare(String a, String b) {
    // Your string ordering logic here. (I won't write that for you.)
  }
});

答案 3 :(得分:2)

实施比较器以比较拼合出的周和年值。使用该比较器对列表进行排序。这应该工作。下面提供了示例实现

class WeekYearComparator implements Comparator throws NumberFormatException{
 public int compare(Object weekYr1, Object weekYr2){

 String[] tokens1 = ((String)weekYr1).split(" ");
 int week1 = Integer.parseInt(tokens1[0].trim());
 int year1 = Integer.parseInt(tokens1[1].trim());

 String[] tokens2 = ((String)weekYr2).split(" ");
 int week2 = Integer.parseInt(tokens2[0].trim());
 int year2 = Integer.parseInt(tokens2[1].trim());

 if( year1 > year2)
  return 1;
 else if( year1 < year2)
  return -1;
 else if(year1 = year2){
   if(week1 > week2)
    return 1;
   if(week1 < week2)
    return -1;
   if(week1 == week2)
    return 0;
 }

}

Arrays.sort(list, new WeekYearComparator);

答案 4 :(得分:1)

List<String> weeks = new ArrayList<String>();
weeks.add(1 + " " + 1958);
weeks.add(32 + " " + 2007);
weeks.add(32 + " " + 1999);

Collections.sort(weeks, new Comparator<String>()
{
  public int compare(String o1, String o2)
  {
    int y1 = Integer.parseInt(o1.substring(o1.indexOf(" ") + 1));
    int y2 = Integer.parseInt(o2.substring(o2.indexOf(" ") + 1));
    if (y1 == y2)
    {
      int w1 = Integer.parseInt(o1.substring(0, o1.indexOf(" ")));
      int w2 = Integer.parseInt(o2.substring(0, o2.indexOf(" ")));
      return w1 - w2;
    }
    return y1 - y2;
  }
});

System.out.println("weeks = " + weeks);

答案 5 :(得分:0)

Arrays.sort(list);

或     Arrays.sort(list,Collections.reverseOrder());

答案 6 :(得分:0)

如果您只想按字符串值排序(按字母顺序排序):

Collections.sort(list);

如果您想按时间顺序排序,可以实现自定义Comparator并将其传递给排序方法:

public class MyComparator implements Comparator<String> {
    public int compare(String first, String second) {
        // Split the string and compare the ints here
    }

    public bool equals(Object o) {
        return this == o;
    }
}

然后:

Collections.sort(list, new MyComparator());

答案 7 :(得分:0)

如果你想按时间顺序排序,你将不得不交换年份和星期,以便它year+" "+week然后进行简单的字母数字排序,或编写自己的Comparator接受list中的两个项目并报告哪个项目是第一个。

另外,请考虑创建一个具有周和年属性的类。使用此类的对象存储您的周/年值,然后您可以以任何您想要的方式输出它,此外还可以更容易排序。

答案 8 :(得分:0)

使用YEARWEEK_OF_YEAR创建日历对象列表。对其进行排序,然后将结果转换为字符串列表。

答案 9 :(得分:0)

一般解决方案:

public static <T> void sort(List<T> list, final List<Comparator<T>> comparatorList) {  
       if (comparatorList.isEmpty()) {//Always equals, if no Comparator.  
            throw new IllegalArgumentException("comparatorList is empty.");  
       }  
       Comparator<T> comparator = new Comparator<T>() {  
       public int compare(T o1, T o2) {  
               for (Comparator<T> c:comparatorList) {  
                   if (c.compare(o1, o2) > 0) {  
                     return 1;  
                   } else if (c.compare(o1, o2) < 0) {  
                     return -1;  
                   }  
               }  
               return 0;  
         }  
       };  
       Collections.sort(list, comparator);  
  }