0

I read a file where every line looks like this: userName,age,award

I need to program two sorting types : by userName and by age. I had no problem with first one, but I have no idea how to sort by age in such example.. I tried switching name with age in each line but it doesn't work. That's what I have ( it's basically only reading from a file and displaying it ):

try{
            File file=new File(path); 
            BufferedReader read=new BufferedReader(new FileReader(file));
            String line=read.readLine();
            all+=line+"\n";
            while(line!=null){
                line=save.readLine();
                all+=line+"\n";
            }

            String [] tabUsers=all.split("\n");       




            String display="";               
            for(int a=0;a<tabUsers.length-1;a++){        
                display+=tabUsers[a]+"\n";
            }

            for(int c=0;c<tabUsers.length-1;c++){
                    System.out.println(tabUsers[c]);

        }


        }
        catch(Exception ex){

        }

Any ideas?

I have tried this, but it did not work :

  for(int b=0;b<tabUsers.length-1;b++){
               String eachLine =tabUsers[b];
                String [] splitLine=eachLine.split(",");
                splitLine[0]=splitLine[1];
            }
3
  • 1
    What do you mean "I know about sorting by name... but I don't by age". I just don't see the difference it makes to sort alphabetically on String or just by natural order relation on int...? Can you show what you are doing for names? Commented Apr 1, 2015 at 11:38
  • As for names I simply use this line: Arrays.sort(tabUsers, 0, tabUsers.length-1); Commented Apr 1, 2015 at 11:43
  • Have you tried to split each line and get the ages for them? If you have it, you can use it to sort, right? Commented Apr 1, 2015 at 11:48

2 Answers 2

1

Read your data into collection of objects like this:

public class User {
    final String name;
    final int age;
    final String award;

    public User(String name, int age, String award) {
        this.name = name;
        this.age = age;
        this.award = award;
    }
}

Then use Collections.sort and Comparator<User> for sorting:

User bob = new User("Bob", 44, null);
User zack = new User("Zack", 13, null);
List<User> users = Arrays.asList(bob, zack);

//sort by age
Collections.sort(users, new Comparator<User>() {
    @Override
    public int compare(User user1, User user2) {
        return Integer.compare(user1.age, user2.age);
    }
});

//sort by name
Collections.sort(users, new Comparator<User>() {
    @Override
    public int compare(User user1, User user2) {
        return user1.name.compareTo(user2.name);
    }
});
Sign up to request clarification or add additional context in comments.

Comments

0

First get the Names and Ages from each line. Then you could put things on two treeMaps, one Names keys and other Age keys. Something like:

    String[] lines = new String[3];
    lines[0] = "Michael 25 Award_1";
    lines[1] = "Ana 15 Award_2";
    lines[2] = "Bruno 50 Award_3";

    String[] parts;
    Map<String, String> linesByName = new TreeMap<String, String>();        
    Map<Integer, String> linesByAge = new TreeMap<Integer, String>();

    for (String line : lines) {
        parts = line.split(" ");
        linesByName.put(parts[0], line);
        linesByAge.put(Integer.parseInt(parts[1]), line);
    }

    System.out.println("Sorted by Name:");

    for (Map.Entry<String, String> entry : linesByName.entrySet()) {
        System.out.println(entry.getValue());
    }

    System.out.println("\nSorted by Age:");

    for (Map.Entry<Integer, String> entry : linesByAge.entrySet()) {
        System.out.println(entry.getValue());
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.