Results 1 to 7 of 7
  1. #1
    ramal is offline Member
    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0

    Default Problem sorting data in a file?

    I have a problem sorting this file alphabetically by second name. Basically, my method sorts each column alphabetically but i would like to sort the file according to the second name. I really really need help. Thanks

    File:
    Moe Carl
    Saul Sergio
    Rocky Louis
    Ike Ziken


    This is how my method sorts the file:

    Ike Carl
    Moe Louis
    Rocky Sergio
    Saul ziken


    Instead
    Moe Carl
    Rocky Louis
    Saul Sergio
    Ike Ziken


    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.*;
    
    
    class thedata implements Comparable 
    	{			
    		private String firstname;
    		private String secondname;
    
    
        public void setFirstname(String firstname) 
        {        
        	    this.firstname = firstname;
        }
    
        public String getFirstname() 
        {       
         	    return firstname;
        }
    
        
    
        public void setSecondname(String secondname) 
        {
                
                this.secondname = secondname;
        }
    
        public String getSecondname() 
        {
                return secondname;
        }
    
        
    
        public int compareTo(Object Student) throws ClassCastException 
        {
                if (!(Student instanceof ShowData))
                throw new ClassCastException("Error");
                String sn = ((ShowData) Student).getSecondname();
                return this.secondname.compareTo(sn); 
        }
    	
    
        
    }
    
    
    
    
    
    
    
    public class sort {
            public static void main(String[] args) {
     
            sortmethod();
       
            }
            
            public static void sortmethod(){
       
    		int j = 0;
    		thedata data[] = new thedata[4];
    		
    
    	    
        	try 
        	{
        		    FileInputStream fstream = new FileInputStream("datafile.txt");
        		    DataInputStream in = new DataInputStream(fstream);
        		    BufferedReader br = new BufferedReader(new InputStreamReader(in));
        
        		    String line;
        		    
        		    String[] firstname = new String[4];
     
        		    String[] secondname = new String[4];
        		    
        		    ArrayList<String> list = new ArrayList<String>();
        		    while ((line = br.readLine()) != null) 
        		    {
        		            list.add(line);
        		            
        		    }
        		    
        		    Iterator<String> itr = list.iterator();
        		    
        		    int k = 0;
        		    for (itr = list.iterator(); itr.hasNext();) 
        		    {
        		            String str = itr.next().toString();
        		            String[] splitSt = str.split("\t");
        		            
        		            
    
    	            for (int i = 0; i < splitSt.length; i++) 
    	            {
    	    				firstname[k] = splitSt[0];
    	    				
    	    				secondname[k] = splitSt[1];
        				   
        		    }
        		    
        		    k++;
        		    }
        		    
        		    
        		    arraysort(firstname);
        		    arraysort(secondname);
        	       	
        	        
        	       	
        	       	for(j = 0;j < 4;j++)
        		    {
    	    				data[j] = new thedata();
    	    				data[j].setFirstname(firstname[j]);
    	    				data[j].setSecondname(secondname[j]);
    	    		}		
    
    
        		    for (int i = 0; i < 4; i++) 
        		    {
    	    				thedata show = data[i];
    	    				String firstname1 = show.getFirstname();
    	    				String secondname1 = show.getSecondname();
    						
    						
    
    						System.out.println(firstname1 + "\t\t" + secondname1);
    
        		    }
        		   
        		    
        					
        
        
        	   		} 
    			   	catch (Exception e) 
    			   	{
    			   		
    				}
        
    }           
        
        
        
        	private static void arraysort(String[] array) 
        	{
        		
        		
        		for(int i=0; i<array.length; i++) 
        		{
        			for(int j=0; j<array.length-1-i; j++) 
        			{
        				if(array[j].compareTo(array[j+1])>0) 
        				{
    
    						String temp1 ="";
                        
    
                            temp1= array[j];//Problem
                            array[j] = array[j+1];
                            array[j+1] = temp1;
                            
    
        				}
        				
        			
        				
        			}
        		}
        
        	}
    
            
    }
    Last edited by ramal; 04-11-2013 at 01:39 PM.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Problem sorting data in a file?

    The order is the same. I don't see a problem!

  3. #3
    ramal is offline Member
    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0

    Default Re: Problem sorting data in a file?

    Quote Originally Posted by Junky View Post
    The order is the same. I don't see a problem!
    No it doesn't work properly. I want to sort the file by second name. When it sorts it, the first name doesn't go with the second name!

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default Re: Problem sorting data in a file?

    Why not simply read in each line from the file, then split it, which will give you the two names, and then create your thedata (terrible name, by the way) object representing that line?

    I don't see the point in creating two arrays first.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    ramal is offline Member
    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0

    Default Re: Problem sorting data in a file?

    Quote Originally Posted by Tolls View Post
    Why not simply read in each line from the file, then split it, which will give you the two names, and then create your thedata (terrible name, by the way) object representing that line?

    I don't see the point in creating two arrays first.

    Thats probably the same thing? How would you do that?

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default Re: Problem sorting data in a file?

    You're already reading the line.
    So rather than store it in a line array, just split, use the two Strings to create your object, and add that to your data array.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    depsai is offline Member
    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0

Similar Threads

  1. Replies: 1
    Last Post: 08-11-2012, 07:38 AM
  2. Replies: 2
    Last Post: 04-27-2011, 05:09 PM
  3. Sorting data
    By yrollgayanth in forum Advanced Java
    Replies: 7
    Last Post: 12-30-2009, 07:16 PM
  4. sorting data in txt file
    By cassysumandak in forum New To Java
    Replies: 1
    Last Post: 04-12-2009, 04:02 AM
  5. Data Sorting in a .data file using java
    By stutiger99 in forum New To Java
    Replies: 2
    Last Post: 10-08-2008, 03:52 AM

Tags for this Thread

Posting Permissions

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