Results 1 to 8 of 8
Thread: Sorting data
- 12-29-2009, 05:07 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 11
- Rep Power
- 0
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?
- 12-29-2009, 05:57 PM #2
Member
- Join Date
- Nov 2009
- Posts
- 23
- Rep Power
- 0
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.
- 12-29-2009, 06:05 PM #3
Member
- Join Date
- Dec 2009
- Posts
- 11
- Rep Power
- 0
ok thanx, but could you please tell me how to apply that,because I've never used it before.
- 12-29-2009, 06:39 PM #4
Member
- Join Date
- Nov 2009
- Posts
- 23
- Rep Power
- 0
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
- 12-29-2009, 06:51 PM #5
Member
- Join Date
- Dec 2009
- Posts
- 11
- Rep Power
- 0
Thanx alot..you are really helpful
- 12-30-2009, 12:35 AM #6
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?"There is no foolproof thing; fools are too smart."
"Why can't you solve my Problem ?"
- 12-30-2009, 03:48 PM #7
Member
- Join Date
- Dec 2009
- Posts
- 11
- Rep Power
- 0
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.92Last edited by yrollgayanth; 12-30-2009 at 03:53 PM.
- 12-30-2009, 06:16 PM #8
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.
The class "Result" describes your data. Provide getter (and maybe setter) and a toString method for your object.Java 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; } });
And the core: the Sorter calss ;)Java 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 ;-) }
It implements the Comparator interface needed.
Only needed if you plan to write a clean code and improve reusability ... like all programmers should :rolleyes:
Java 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; } }Last edited by AndreB; 12-30-2009 at 06:20 PM. Reason: omg, so many spelling errors :-(
"There is no foolproof thing; fools are too smart."
"Why can't you solve my Problem ?"
Similar Threads
-
Sorting data in a Vector
By SBL in forum AWT / SwingReplies: 11Last Post: 11-27-2009, 03:20 AM -
sorting data in txt file
By cassysumandak in forum New To JavaReplies: 1Last Post: 04-12-2009, 03:02 AM -
Data Pipeline 2 - Data Transformation Toolkit for Java Released
By dele in forum Java SoftwareReplies: 0Last Post: 10-31-2008, 02:13 PM -
Data Sorting in a .data file using java
By stutiger99 in forum New To JavaReplies: 2Last Post: 10-08-2008, 02:52 AM -
sorting
By jot321 in forum New To JavaReplies: 18Last Post: 10-02-2008, 10:30 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks