-
Sorting data
Hello every1. I'm having a problem sorting data which reads from a text file.
Data in the text file as follows
1 334 John Steffensen AUS 44.82
2 500 Takeshi Fujiwara ESA 46.92
3 651 Dimitrios Rogas GRE 46.22
4 352 Chris Brown BAH 44.5
5 1050 Rennyuow Santa TRI 45.7
6 491 Arismendy Peguero DOM 44.92
7 897 Marcin Marciniszyn POL 45.83
8 626 Bastian Swillims GER 45.44
When program runs i want data to be sorted out in ascending order correspond to the lap time as follows.
4 352 Chris Brown BAH 44.5
1 334 John Steffensen AUS 44.82
6 491 Arismendy Peguero DOM 44.92
8 626 Bastian Swillims GER 45.44
5 1050 Rennyuow Santa TRI 45.7
7 897 Marcin Marciniszyn POL 45.83
3 651 Dimitrios Rogas GRE 46.22
2 500 Takeshi Fujiwara ESA 46.92
My code so far
import java.io.*;
import java.util.*;
public class Main {
public static class readfile{
private Scanner x;
public void openfile()throws java.io.IOException{
try{
FileReader fr = new FileReader("Lap.dat");
x = new Scanner(fr);
}
catch(Exception e){
System.out.println("Could not locate the file"+e);
}
}
public void readfile(){
int lane ;
int number ;
String firstname ;
String surname ;
String country ;
double laptime ;
while(x.hasNext()){ //as long as data contain in file
lane = x.nextInt();
number = x.nextInt();
firstname = x.next();
surname = x.next();
country = x.next();
laptime = x.nextDouble();
System.out.printf("%s %s %s %s %s %s\n",lane,number,firstname,surname,country,laptim e);
}
}
public void closefile(){
x.close();
}
}
public static void main(String[] args)throws java.io.IOException {
readfile r = new readfile();
r.openfile();
r.readfile();
r.closefile();
}
}
Can someone help me to do that please?
-
When you read in from the file Store each line in a HashMap with the lap time as the key. Then instead of using printf just loop through your HashMap and have it sort according to the keyValues.
-
ok thanx, but could you please tell me how to apply that,because I've never used it before.
-
HashMap (Java 2 Platform SE v1.4.2)
HashMap<Double,String> map = new HashMap<Double,String>();
while(x.hasNext()){
lane = x.nextInt();
number = x.nextInt();
firstname = x.next();
surname = x.next();
country = x.next();
laptime = x.nextDouble();
map.put(laptime,lane+number+firstname+surname+coun try+laptime);
}
Map<Double, String> sortedMap = new TreeMap<Double, String>(map);
System.out.println(sortedMap);
that looks good
-
Thanx alot..you are really helpful
-
Or you can do some generic variant:
Create a object represting a row (number, name, nationality, time) and the create an array representation of your data set.
Then you can use the Arrays.*sort* method with your object and a Compartor interface. This interface may then sort based on your preferences.
Need a code?
-
yes if you can give me the code that would be great..And i need only the first three results to be printed as the output as following..
4 352 Chris Brown BAH 44.5
1 334 John Steffensen AUS 44.82
6 491 Arismendy Peguero DOM 44.92
-
Here some sample code (far from complete, but thats your job, isn't it?)... if you have questions, ask ;-)
The sorting calss would look like this.
Code:
//read data... translate to objects...
//create array (from list or something other)
Result[] myArray = {new Result()};
// sorting given a reusable sorter (own implementation)
Arrays.sort(myArray, new MySorter(MySorter.SortIndicator.time, -1));
// or as anonymous class (sorting by time)
//note, that the sorting happens on the given reference.
//no new object array will be returned and the old sorting gets lost
Arrays.sort(myArray, new Comparator<Result>() {
@Override
public int compare(Result o1, Result o2) {
int result = 0;
if (o1.getTime() > o2.getTime()) {
result = 1;
} else if (o1.getTime() < o2.getTime()) {
result = -1;
}
return result;
}
});
The class "Result" describes your data. Provide getter (and maybe setter) and a toString method for your object.
Code:
public static class Result {
// note that atomic types also should be objects
private String name;
private Integer number1;
private Integer number2;
private String nationality;
private Double time;
public Double getTime() {
return time;
}
public String getName() {
return name;
}
//create getter/setter
//create a suitable to string method ;-)
}
And the core: the Sorter calss ;)
It implements the Comparator interface needed.
Only needed if you plan to write a clean code and improve reusability ... like all programmers should :rolleyes:
Code:
public static class MySorter implements Comparator<Result> {
//what column to sort ?
public static enum SortIndicator {
name,
number1,
number2,
nationality,
time
}
private SortIndicator indicator;
// in what sort direction? (-1 = downwards / 1 = upwards)
private int sortDirection;
public MySorter(SortIndicator indicator, int dir) {
this.indicator = indicator;
this.sortDirection = dir;
}
@Override
public int compare(Result o1, Result o2) {
int result = 0;
// based on what we want to compare we have to get different values
// if the returned objects additionally provide compareTo method the better!
switch (indicator) {
case time:
if (o1.getTime() > o2.getTime()) {
result = 1;
} else if (o1.getTime() < o2.getTime()) {
result = -1;
}
break;
case name:
result = o1.getName().compareTo(o2.getName());
break;
case nationality:
// ...
case number1:
case number2:
default:
break;
}
return sortDirection*result;
}
}