Results 1 to 8 of 8

Thread: Sorting data

  1. #1
    yrollgayanth is offline Member
    Join Date
    Dec 2009
    Posts
    11
    Rep Power
    0

    Default 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?

  2. #2
    Join Date
    Nov 2009
    Posts
    23
    Rep Power
    0

    Default

    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.

  3. #3
    yrollgayanth is offline Member
    Join Date
    Dec 2009
    Posts
    11
    Rep Power
    0

    Default

    ok thanx, but could you please tell me how to apply that,because I've never used it before.

  4. #4
    Join Date
    Nov 2009
    Posts
    23
    Rep Power
    0

    Default

    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

  5. #5
    yrollgayanth is offline Member
    Join Date
    Dec 2009
    Posts
    11
    Rep Power
    0

    Default

    Thanx alot..you are really helpful

  6. #6
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

    Default

    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 ?"

  7. #7
    yrollgayanth is offline Member
    Join Date
    Dec 2009
    Posts
    11
    Rep Power
    0

    Default

    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
    Last edited by yrollgayanth; 12-30-2009 at 03:53 PM.

  8. #8
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

    Default

    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.
    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;
    	}
    });
    The class "Result" describes your data. Provide getter (and maybe setter) and a toString method for your object.

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

    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

  1. Sorting data in a Vector
    By SBL in forum AWT / Swing
    Replies: 11
    Last Post: 11-27-2009, 03:20 AM
  2. sorting data in txt file
    By cassysumandak in forum New To Java
    Replies: 1
    Last Post: 04-12-2009, 03:02 AM
  3. Replies: 0
    Last Post: 10-31-2008, 02:13 PM
  4. Data Sorting in a .data file using java
    By stutiger99 in forum New To Java
    Replies: 2
    Last Post: 10-08-2008, 02:52 AM
  5. sorting
    By jot321 in forum New To Java
    Replies: 18
    Last Post: 10-02-2008, 10:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •