2013-03-26 59 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