read your file with readLine() and use split to get every field of your data, for example:
BufferedReader br = new BufferedReader(new FileReader("FileName")); String line = null; ArrayList<Person> list = new ArrayList<Person>(); while ((line = br.readLine()) != null) { String[] fields = line.split(","); list.add(new Person(fields[0], fields[1], Integer.parseInt(fields[2]))); }
Then you can save your data in an ArrayList , which takes its own class, for example. Person , which stores information about a person and implements Comparable , where you execute sorting logic.
If you need to group your data by person’s name, you might consider having a Hashtable , where the key is the person’s name and the value is an ArrayList experience.
You can define a class for your data, for example.
class Person implements Comparable<Person> { private String name; private String company; private int experience; public Person(String name, String company, int experience) { this.name = name; this.company = company; this.experience = experience; } public int getExperience() { return experience; } @Override public int compareTo(Person person) { return new Integer(experience).compareTo(person.getExperience()); } }
To sort your list, just call Collections.sort(list); ; however, this list will contain all the data, so change the code to group the data by employee name and specify a list for each employee.
source share