Sort ArrayList faces with java collections

Below the code that I use works fine and displays the names, except that the sorting method does not work. I was expecting "Collections.sort (nameFromText)"; to sort ArrayList alphabetically by name.

What am I doing wrong?

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(); } 
+8
source share
5 answers

Collections.sort(List<T>) method expects the list item that it sorts to be comparable. Either the element type T must implement the Comparable interface, or you must use the overloaded sort() , which accepts a generic Comparator instance.

In the code below, you do not satisfy any of the above conditions. Neither your Person class implements Comparable , nor do you pass an instance of Comparator .

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

You should create a Comparator for your Person class to tell the sort() method how to sort it (maybe in a String stored in the Person class)

Here you implement the general comparator:

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

And then your call to the Collections.sort() method should look like this: -

 Collections.sort(nameFromText, new PersonNameComparator()); 
+28
source

Alternatively, you can implement the Comparable interface directly in the Person class and override the compareTo (Object obj) method. In this case, you do not need to create a new class for the comparator. And it behaves like an inline sort.

+4
source

Try the following:

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

use 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); } 
0
source

If you are using Java 8 then use one lambda expression liner

 ArrayList<Person> nameFromText = new ArrayList<Person>(); fillArrayList(nameFromText, pullFile); nameFromText.sort((p1, p2) -> p1.getName().compareTo(p2.getName())); 
0
source

All Articles