chatteVerte
chatteVerte

Reputation: 13

Sorting by index java

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];
            }

Upvotes: 0

Views: 79

Answers (2)

Hugo Oshiro
Hugo Oshiro

Reputation: 380

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());
    }

Upvotes: 0

Marek Adamek
Marek Adamek

Reputation: 520

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);
    }
});

Upvotes: 1

Related Questions