2013-03-26 27 views
1

我试图理清按姓氏排列,我已经在我的代码撞了南墙排序,我不知道该怎么办,我可以”找到任何可以帮助我的东西。 我的主要问题是在按姓氏在联系人列表阵列

 public void sortByLastName(){ 
     Collections.sort(list); 
    } 

这两段代码是两个不同的类,是有问题吗?

public int compareTo(Person p){ 
    int compareResult = this.lastName.compareTo(p.lastName); 
    if(compareResult == 0){ 
     return 0; 
    } 
     else 
      if(compareResult > 0){ 
       return 1; 
      } 
      else 
       return -1; 
    } 
} 
+2

这可以被改写将整理我的项目清单(产品模型类)为'返回this.lastName.compareTo(p.lastName);' – jlordo 2013-03-26 00:23:20

+1

假设lastName的是一个字符串,你可以这样做:返回this.lastName.toLower()的compareTo(p.lastName.toLower());并保存自己的所有其他代码。不过,字符串中的比较是区分大小写的,所以要小心。 – Kylar 2013-03-26 00:24:44

回答

3

如果你希望在排序方式不止一种东西,你会很快确定,这样做的最好的事情是要通过你的比较函数作为参数传递给Collections.sort

public void sortByLastName(){ 
    Collections.sort(list, new Comparator<Person>() { 
     public int compare(Person lhs, Person rhs){ 
      return lhs.lastName.compareTo(rhs.lastName); 
     } 
    }); 
} 
0

你可以试试:

public int compareTo(Person p){ 
    return lastName.compareToIgnoreCase(p.lastName); 
} 
0

compareTo方法需要在你的Person类。您的Person课程需要implement Comparable<Person>。然后Collections.sort将工作 -

private static final class Person implements Comparable<Person> { 

    private String firstName; 
    private String lastName; 

    public Person(String firstName, String lastName) { 
     this.firstName = firstName; 
     this.lastName = lastName; 
    } 

    public String getFirstName() { 
     return firstName; 
    } 

    public void setFirstName(String firstName) { 
     this.firstName = firstName; 
    } 

    public String getLastName() { 
     return lastName; 
    } 

    public void setLastName(String lastName) { 
     this.lastName = lastName; 
    } 

    public int compareTo(Person o) { 
     int c = getLastName().compareTo(o.getLastName()); 
     if (c != 0) { 
      return c; 
     } 
     return getFirstName().compareTo(o.getFirstName()); 
    } 

    @Override 
    public String toString() { 
     return getFirstName() + " " + getLastName(); 
    } 

} 

public static void main(String[] args) { 
    final List<Person> people = new LinkedList<Person>(); 
    people.add(new Person("John", "Smith")); 
    people.add(new Person("Hans", "Mustermann")); 
    people.add(new Person("John", "Doe")); 
    System.out.println(people); 
    Collections.sort(people); 
    System.out.println(people); 
} 

输出:

[John Smith, Hans Mustermann, John Doe] 
[John Doe, Hans Mustermann, John Smith] 
-1

我做了一个功能通过在模型类特定变量排序对象的列表。它可能不会直接匹配你需要的,但也许你可以接受这个想法。我使用反射来排序任何数据类型的类模型。

public Vector sort(Vector list, String kelas, String s, String asc) throws NoSuchMethodException { 

     try { 
      // Creates an object of type Class which contains the information of 
      // the class String 
      Object[] obj = list.toArray(); 
      Object[] args = {}; 
      Class cl = Class.forName(kelas); 
      Method toSort = cl.getMethod(s, null); 
      if (asc.equalsIgnoreCase("desc")) { 
       if (toSort.getReturnType().toString().equalsIgnoreCase("int") || toSort.getReturnType().toString().equalsIgnoreCase("double")) { 
        for (int i = 0; i < obj.length; i++) { 
         for (int j = i; j < obj.length; j++) { 
          try { 
           if (Double.parseDouble(toSort.invoke(obj[i], args).toString()) < Double.parseDouble(toSort.invoke(obj[j], args).toString())) { 
            Object temp = obj[i]; 
            obj[i] = obj[j]; 
            obj[j] = temp; 
           } 
          } catch (IllegalAccessException ex) { 
           Logger.getLogger(DbBean.class.getName()).log(Level.SEVERE, null, ex); 
          } catch (IllegalArgumentException ex) { 
           Logger.getLogger(DbBean.class.getName()).log(Level.SEVERE, null, ex); 
          } catch (InvocationTargetException ex) { 
           Logger.getLogger(DbBean.class.getName()).log(Level.SEVERE, null, ex); 
          } 
         } 
        } 
       } else { 
        for (int i = 0; i < obj.length; i++) { 
         for (int j = i; j < obj.length; j++) { 
          try { 
           if (toSort.invoke(obj[i], args).toString().compareToIgnoreCase(toSort.invoke(obj[j], args).toString()) < 0) { 
            Object temp = obj[i]; 
            obj[i] = obj[j]; 
            obj[j] = temp; 
           } 
          } catch (IllegalAccessException ex) { 
           Logger.getLogger(DbBean.class.getName()).log(Level.SEVERE, null, ex); 
          } catch (IllegalArgumentException ex) { 
           Logger.getLogger(DbBean.class.getName()).log(Level.SEVERE, null, ex); 
          } catch (InvocationTargetException ex) { 
           Logger.getLogger(DbBean.class.getName()).log(Level.SEVERE, null, ex); 
          } 
         } 
        } 
       } 
      } else { 
       if (toSort.getReturnType().toString().equalsIgnoreCase("int") || toSort.getReturnType().toString().equalsIgnoreCase("double")) { 
        for (int i = 0; i < obj.length; i++) { 
         for (int j = i; j < obj.length; j++) { 
          try { 
           if (Double.parseDouble(toSort.invoke(obj[i], args).toString()) > Double.parseDouble(toSort.invoke(obj[j], args).toString())) { 
            Object temp = obj[i]; 
            obj[i] = obj[j]; 
            obj[j] = temp; 
           } 
          } catch (IllegalAccessException ex) { 
           Logger.getLogger(DbBean.class.getName()).log(Level.SEVERE, null, ex); 
          } catch (IllegalArgumentException ex) { 
           Logger.getLogger(DbBean.class.getName()).log(Level.SEVERE, null, ex); 
          } catch (InvocationTargetException ex) { 
           Logger.getLogger(DbBean.class.getName()).log(Level.SEVERE, null, ex); 
          } 
         } 
        } 
       } else { 
        for (int i = 0; i < obj.length; i++) { 
         for (int j = i; j < obj.length; j++) { 
          try { 
           if (toSort.invoke(obj[i], args).toString().compareToIgnoreCase(toSort.invoke(obj[j], args).toString()) > 0) { 
            Object temp = obj[i]; 
            obj[i] = obj[j]; 
            obj[j] = temp; 
           } 
          } catch (IllegalAccessException ex) { 
           Logger.getLogger(DbBean.class.getName()).log(Level.SEVERE, null, ex); 
          } catch (IllegalArgumentException ex) { 
           Logger.getLogger(DbBean.class.getName()).log(Level.SEVERE, null, ex); 
          } catch (InvocationTargetException ex) { 
           Logger.getLogger(DbBean.class.getName()).log(Level.SEVERE, null, ex); 
          } 
         } 
        } 
       } 
      } 

      list = new Vector(); 
      for (int i = 0; i < obj.length; i++) { 
       list.add(obj[i]); 
      } 
     } catch (ClassNotFoundException e) { 
      e.printStackTrace(); 
     } 
     return list; 
    } 

你可以调用该函数像这样简单:

Vector sortedList=sort(UnsortedList, "bean.Items", "getItemName", "asc"); 

该行基于项目名称升

+0

这不仅比'Collections.sort'方法慢,它也更加混乱。此外,它使用“同步”的“Vector”。这不是一个好方法。 -1。 – 2013-03-26 11:26:31