比较器排序

时间:2013-08-19 21:34:09

标签: java string sorting comparator

我对比较器不太好但是我需要学习它,到目前为止我已经完成了这么多但是我一直在'System.out.println'得到错误(compy(“January”,“March”,“October) “,”四月“));'...请帮忙,我想解释一下。谢谢!

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Months {
    public static void main (String args[]){

        System.out.println(Comp("January","May"));
        System.out.println(Comp("October", "May"));
        System.out.println(Comp("August","August"));
        System.out.println(Comp("January", "March"));

        System.out.println(compy("January", "March", "October", "April"));
    }

    static int Comp(String s1, String s2){

        while (true){
            int r = 0;
            int s = 0;

            if(s1.equals("January")){
                r=1;
            }

            if(s1.equals("February")){
                r=2;
            }

            if(s1.equals("March")){
                r=3;
            }

            if(s1.equals("April")){
                r=4;
            }

            if(s1.equals("May")){
                r=5;
            }

            if(s1.equals("June")){
                r=6;
            }

            if(s1.equals("July")){
                r=7;
            } 

            if(s1.equals("August")){
                r=8;
            }

            if(s1.equals("September")){
                r=9;
            }

            if(s1.equals("October")){
                r=10;
            }

            if(s1.equals("November")){
                r=11;
            }

            if(s1.equals("December")){
                r=12;
            }

            if(s2.equals("January")){
                s=1;
            }

            if(s2.equals("February")){
                s=2;
            }

            if(s2.equals("March")){
                s=3;
            }

            if(s2.equals("April")){
                s=4;
            }

            if(s2.equals("May")){
                s=5;
            }

            if(s2.equals("June")){
                s=6;
            }

            if(s2.equals("July")){
                s=7;
            }

            if(s2.equals("August")){
                s=8;
            }

            if(s2.equals("September")){
                s=9;
            }

            if(s2.equals("October")){
                s=10;
            }

            if(s2.equals("November")){
                s=11;
            }

            if(s2.equals("December")){
                s=12;
            }

            if(r<s){
                return -1;
            }

            if(r>s){
                return 1;
            }
            if(r==s){
                return 0;
            }

        }   

    }

    public class Comp implements Comparator {
        public int compare(Object o1, Object o2){
            return Comp((String)o1, (String)o2);
        }
    }

    void sort(List l){
        Comparator compy = new Comp();
        Collections.sort(l, compy);
    }

}

3 个答案:

答案 0 :(得分:3)

这是您使用代码对列表进行排序的方法。

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

list.add("March");
list.add("January");
list.add("October");
list.add("April");

System.out.println("Before sort: " + list);
// prints: Before sort: [March, January, October, April]

new Months().sort(list); // sort

System.out.println("After sort: " + list);
// prints: After sort: [January, March, April, October]

答案 1 :(得分:2)

Ravi Thapliyal为您提供了很好的解决方案,但如果我能提出建议,请更换您的比较器

public class Comp implements Comparator<String> {

   private List<String> months = Arrays.asList("January" , "February",...);

    @Override
    public int compare(String s1, String s2) {
        return months.indexOf(s1)-months.indexOf(s2);
    }
}

它更短更可读

答案 2 :(得分:0)

包括实际错误会有所帮助。

在我看来,错误是你在sort方法中编译变量。从主要方法。

尝试类似

的内容
package com.example;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class HomeWork {

  public static void main(String args[]) {
    MonthComparable compy = new MonthComparable();
    List<String> myList = Arrays.asList(new String[] {"May", "June", "April"});
    Collections.sort(myList, compy);
    System.out.println();
  }

  public static class MonthComparable implements Comparator<String> {

    @Override
    public int compare(String left, String right) {
      Integer leftValue = monthValue(left);
      Integer rightValue = monthValue(right);
      return leftValue.compareTo(rightValue);
    }

    private static int monthValue(String left) {
      // TODO Implement this
      return 0;
    }
  }
}