Java USES Comparable to solve the sorting problem

  • 2020-04-01 03:52:35
  • OfStack

This article illustrates how Java USES Comparable to solve a sort problem. Share with you for your reference. The specific implementation method is as follows:

The rules of a weightlifting competition are: the results of the athletes in order of the total weight of successful lifting, lifting the total weight of the first; When lifting the same total weight, in order of weight, light weight ranked first; The program is required to read the data file as input, and in accordance with the rules listed above, print out the number of players; Data file description is as follows: there are 5 players, their number, total weight successfully lifted and their weight, such as data file data4.txt, sample contents are as follows:


<p>
<no>1</no>
<lw>140</lw>
<bw>54</bw>
</p>
<p>
<no>2</no>
<lw>155</lw>
<bw>53</bw>
  </p>
<p>
<no>3</no>
<lw>140</lw>
<bw>42</bw>
  </p>
<p>
<no>4</no>
<lw>140</lw>
<bw>55</bw>
  </p>
<p>
<no>5</no>
<lw>130</lw>
<bw>46</bw>
</p>

The first thing I want to solve is the problem of file parsing:

How to parse the document into the desired data: extract the number, score and weight of each contestant
I use an entity Person to encapsulate these properties

Overall code:


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
public class forth {
public static void main(String[] args) {
ArrayList<Person> list=new ArrayList<Person>();
try {
FileReader fr=new FileReader("c:\data.txt");
BufferedReader br=new BufferedReader(fr);
String str=null;
int num=0;
int score=0;
int weight=0;
int i=0;
while((str=br.readLine())!=null)
{
  i++;
  if(i%5==2)
  {
str=str.trim().substring(4,str.length()-5);
num=Integer.parseInt(str);
str=br.readLine().trim();
str=str.substring(4,str.length()-5);
score=Integer.parseInt(str);
i++;
str=br.readLine().trim();
str=str.substring(4,str.length()-5);
weight=Integer.parseInt(str);
i++;
Person p=new Person(num,score,weight);
list.add(p);
  }
  else 
  continue;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
Person[] plist=new Person[list.size()];
list.toArray(plist);
Arrays.sort(plist);
for(int i=0;i<plist.length;i++)
{
System.out.print(plist[i].getNum()+". " +plist[i].getScore()+" "+plist[i].getWeight()+"nr");
}
}
}
class Person implements Comparable<Person>{
private int num;
private int weight;
private int score;
public Person(int num,int score,int weight){
this.num=num;
this.score=score;
this.weight=weight;
}
@Override
public int compareTo(Person other) {
if(this.score>other.score)return -1;
  else if(this.score<other.score) return 1;
  else
  return this.weight>other.weight?1:-1;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}

I hope this article has been helpful to your Java programming.


Related articles: