2012-10-07 91 views
7

在我使用的代碼下面工作得很好,並輸出除排序方法無效的名稱外。我期望「Collections.sort(nameFromText);」按名稱的字母順序排列ArrayList。使用Java集合對PersonList的ArrayList進行排序

我在做什麼錯?

public static void main(String[] args) throws IOException { 
    // Create and populate text file 
    Writer textFile = new FileWriter("names.txt"); 
    String[] nameArray = new String[] { "Tina Tully\n", "Bill Simpson\n", 
      "Dana Smith\n", "Ralph Andrews\n", "Greg Smithers\n", 
      "Lisa Krump\n", "Gill Bitters\n", "Barbara West\n", 
      "Sandra McDonald\n", "Bart Willis\n", "Bucky Zimmerman\n", 
      "Richard Vicks\n", "Velma Tarp\n", "Winslow Tunnell\n", 
      "Andrew Letterman\n", "Betty Trump\n", "Waldo Smith\n", 
      "Kyle Ronno\n", "Vivian West\n", "Wendy Tunnell\n" }; 
    generateText(textFile, nameArray); 

    // Create object of previously created text file 
    Scanner pullFile = new Scanner(new File("names.txt")); 

    // Create 20 Person Objects and add to ArrayList data structure with 
    // name variables assigned to values from text file 
    ArrayList<Person> nameFromText = new ArrayList<Person>(); 
    fillArrayList(nameFromText, pullFile); 

    // Sort ArrayList 
    Collections.sort(nameFromText); 

    // Print ArrayList 
    printNamesFromObjects(nameFromText); 
} 

private static void printNamesFromObjects(ArrayList<Person> namesFromText) { 
    for (int i = 0; i < 20; i++) { 
     System.out.println(namesFromText.get(i).name); 
    } 
} 

private static void fillArrayList(ArrayList<Person> nameFromText, 
     Scanner pullFile) { 
    while (pullFile.hasNext()) { 
     Person obj = new Person(pullFile.nextLine()); 
     nameFromText.add(obj); 
    } 
} 

private static void generateText(Writer textFile, String[] nameArray) 
     throws IOException { 
    for (int i = 0; i < 20; i++) { 
     textFile.write(new String(nameArray[i])); 
    } 
    textFile.close(); 
} 
+0

你會發現這個有用的。 http://www.thejavageek.com/2013/06/17/sorting-user-defined-objects-part-1/和http://www.thejavageek.com/2013/06/17/sorting-user-defined -objects-part-2/ –

回答

26

Collections.sort(List<T>)方法期望它所排序列表的元素具有可比性。元素類型T應該實現Comparable接口,或者您應該使用重載的sort()方法,該方法需要一個通用的Comparator實例。

在下面的代碼中,您既不滿足上述條件。您的Person類別都不實施Comparable,也不通過任何Comparator實例。

ArrayList<Person> nameFromText = new ArrayList<Person>(); 
fillArrayList(nameFromText, pullFile); 
// Sort ArrayList 
Collections.sort(nameFromText); // How to sort? 

你應該創建一個Comparator爲您Person類告訴sort()方法如何對它進行排序(可能是存儲在Person類的字符串)

這裏是你如何實現一個通用的比較:

public class PersonNameComparator implements Comparator<Person> { 
    public int compare(Person p1, Person p2) { 
     return p1.getName().compareTo(p2.getName()); 
    } 
} 

然後你Collections.sort()方法調用應該是這樣的: -

Collections.sort(nameFromText, new PersonNameComparator()); 
+1

@DavidTunnell Your're welcome ..你也可以google - 「Comparator和Comparable之間的區別」以獲取更多信息。'Comparable'也用於此目的。但是'Comparator'是因爲你可以創建多個排序邏輯.. –

+0

我會這樣做謝謝 –

+0

我認爲你的代碼有一個錯字,不應該是:'...(Person)o' ** 1 **' ; ... Person p2 =(Person)o' ** 2 **';'? –

3

或者,您可以直接在Person類中實現Comparable接口並重寫'compareTo(Object obj)'方法。在這種情況下,您不需要爲比較器創建新類。這就像內置排序一樣。

+1

+1這就是一個有效的點。但是你應該比'Comparable'更喜歡'Comparator'。這樣你就可以有多種排序邏輯對於你的班級。 –

+0

是的,比較給你彈性時有多個排序候選人。 –

2

嘗試這種情況:

List<String> inputString = Arrays.asList("Sijan", "Sudeep", "Parasar", "Raj Kumar"); 
Collections.sort(inputString); 
System.out.println(inputString); 
0

使用Collections.sort(managerNameList);

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

    managerNameList.add("antesh"); 
    managerNameList.add("Lalit"); 
    managerNameList.add("Gokul"); 
    managerNameList.add("Ajay"); 

    System.out.println("Arraylist before sorting");  
    for(String name: managerNameList) 
    {  
     System.out.println(name);   
    } 

    Collections.sort(managerNameList); 

    System.out.println("Arraylist after sorting");  
    for(String name: managerNameList) 
    {  
     System.out.println(name);   
    } 
相關問題