6 Replies Latest reply: Apr 11, 2013 5:34 PM by EJP RSS

    Problem sorting data in a file?

    1002366
      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



      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;
                               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];
      array[j] = array[j+1];
      array[j+1] = temp1;


                          }
                          
                     
                          
                     }
                }

           }


      }






      Edited by: 999363 on Apr 11, 2013 3:41 AM