0

Hi all I want to sort 2 related files, creating an unique index which can sort both of them

I have a .txt file formatted as the following sample:

File1

Houston
Chicago
Seattle
Cleveland

The other file is formatted like this:

File2

44
33
55
22

I want the following output:

Seattle 55
Houston 44
Chicago 33
Cleveland 22

I've created 2 array of objects from the .txt files, so I can't use bubblesort because of the "< or >" operator. I've used .sort function to sort the scores but in this way I don't create an index to sort the teams. How can I solve the problem?

That's my code

 private void visualizzaClassificaButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                           
    // TODO add your handling code here:
    List<String> teams = new ArrayList<String>();
    List<Float> scores = new ArrayList<Float>();

    try {
        FileInputStream fstream = new FileInputStream("C:/Users/Fra....../file1");
        FileInputStream fstream2 = new FileInputStream("C:/Users/Fra...../file2");
        DataInputStream in = new DataInputStream(fstream);
        DataInputStream in2 = new DataInputStream(fstream2);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        BufferedReader br2 = new BufferedReader(new InputStreamReader(in2));
        String team = null;
        String score = null;

        while ((score = br.readLine()) != null && (team = br2.readLine()) !=null)   {
            teams.add(team);
            scores.add(Float.parseFloat(score));
        }

        Object[] squadreTutte = teams.toArray();
        Object[] punteggiTutti = scores.toArray();
        Arrays.sort(punteggiTutti, Collections.reverseOrder());
        //????????????????????????????????
        for(int index=0; index<punteggiTutti.length; index++){
            System.out.println(punteggiTutti[index]);
            System.out.println(squadreTutte[index]);
        }
        in.close();
    } catch (Exception e) {
    }

}

Please help

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Fseee
  • 2,476
  • 9
  • 40
  • 63

3 Answers3

2

Almost always when someone wants to sort parallel arrays, it's a case of object denial.

What you should be doing is to create an object to hold the name,score pair:

public class Team implements Comparable<Team> {
  private final String name;
  private final int score;

  public Team(final String name, final int score) {
    this.name=name;
    this.score=score;
  }

  // add getters

  public int compareTo(Team other) {
    if (this.score != other.score) {
      return Integer.valueOf(this.score).compareTo(other.score);
    } else {
      return this.name.compareTo(other.name);
    }
  }
}

Then, when reading the files, create a Team object for each line in the files, put them into a Collection and sort that. You either need to implement Comparable in your class (as I did above) or provide a Comparator to sort them.

Community
  • 1
  • 1
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
1

It is a lot simpler to sort the team and score in natrual worder and read the collection is reverse order. BTW Don't use float or Float unless there is a really good reason to do so. Using Long or Double instead.

Based on @Joachim's Team class

List<Team> teams = new ArrayList<Team>();

BufferedReader br=new BufferedReader(new FileReader("C:/Users/Fra..../file1"));
BufferedReader br2=new BufferedReader(new FileReader("C:/Users/Fra..../file2"));

String name = null;
String score = null;
while ((score = br.readLine()) != null && (name = br2.readLine()) != null) 
    teams.add(new Team(name, Double.parseDouble(score)));
br.close();
br2.close();

// reverse sort.
Collections.sort(teams, new Comparator<Team>() {
    public int compare(Team t1, Team t2) {
        return Double.compare(t2.score, t1.score);
    }
});


for(Team t: teams)
   System.out.println(t.name + ' ' + t.score);


public class Team {
  final String name;
  final double score;

  public Team(String name, double score) {
    this.name=name;
    this.score=score;
  }
}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

The goal is that the teams in the first list are sorted by score from the second list ?

Your problem is that the sorting of both lists are unrelated. You should try instead to sort the couples "team+score". Maybe using something like a List<Entry<Double, String>> could do what you want by defining a custom comparator...

Vinze
  • 2,549
  • 3
  • 22
  • 23