Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding objects to arraylist and sorting it
    primarykey
    data
    text
    <p>I wrote a program in which I add objects to an arraylist and then sort it by ID. Here's my program:</p> <pre><code>package TestPackage; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; import com.fulcrum.emp.EmployeeSortById; public class EmployeeSorting { // path till 'employee files' folder. File folder = new File("D:\\Arthi iyer\\employee files"); // listFiles() : list all the files in a folder. File[] listOfFiles = folder.listFiles(); ArrayList&lt;Employee&gt; emp = new ArrayList&lt;Employee&gt;(); String[] split_input = null; public void sortFiles() throws FileNotFoundException { for (File file : listOfFiles) { Scanner scanner = new Scanner(file); String input = scanner.nextLine(); split_input = input.split("="); int id = Integer.parseInt(split_input[1]); String input1 = scanner.nextLine(); split_input = input1.split("="); String name = split_input[1]; String input2 = scanner.nextLine(); split_input = input2.split("="); int age = Integer.parseInt(split_input[1]); // Employee e=new Employee(id, name, age); // System.out.println(e); emp.add(new Employee(id, name, age)); // System.out.println(emp.size()); // for(int i=0;i&lt;emp.size();i++) // { // System.out.println(emp.get(i) +""+i); // } }// for ends }// method ends public void sortByID() { System.out.println("----Sort By Employee Id----"); Collections.sort(emp, new EmployeeSortById()); } public static void main(String[] args) throws FileNotFoundException { EmployeeSorting sort = new EmployeeSorting(); sort.sortFiles(); sort.sortByID(); } } </code></pre> <p>My problem is it gives an error saying:</p> <blockquote> <p>The method sort(List, Comparator) in the type Collections is not applicable for the arguments (ArrayList, EmployeeSortById)</p> </blockquote> <p>But in my <code>EmployeeSortById</code> class I have implemented <code>Comparator&lt;Employee&gt;</code> properly. Still it's giving problems. Can anyone please guide me?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload