Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Array List Sorting

807589Oct 17 2008 — edited Oct 26 2008
Hi,

Can anyone please give me an hint on how to sort an arraylist which contains contents of multiple files. For example, the file has different columns like first name, lastname, sex, date of birth. lastname is the second column and I would like to sort the arraylist according to the lastname.

The code is as follows:
public class test {
    
    public static void main(String[] args)
    {
        File file = new File("file1.txt");
        File file1 = new File("file2.txt");
        File file2 = new File("file3.txt");
        ArrayList filelist = new ArrayList();
        try
        {
            FileReader fr = new FileReader(file);
            BufferedReader br = new BufferedReader(fr);
            
            while(br.ready())
            {
                String line = br.readLine();
               // System.out.println(line);
                filelist.add(line);
            }
            
        }
        catch(IOException ioe)
        {
            ioe.printStackTrace();
        }
        try
        {
            FileReader fr1 = new FileReader(file1);
            BufferedReader br1 = new BufferedReader(fr1);
            
            while(br1.ready())
            {
                String line1 = br1.readLine();
                //System.out.println(line1);
                filelist.add(line1);
            }
            
        }
        catch(IOException ioe)
        {
            ioe.printStackTrace();
        }
        try
        {
            FileReader fr3 = new FileReader(file2);
            BufferedReader br3 = new BufferedReader(fr3);
            
            while(br3.ready())
            {
                String line3 = br3.readLine();
                //System.out.println(line3);
                filelist.add(line3);
            }
             Collections.sort(filelist);
                 Iterator it= filelist.iterator();
                 while(it.hasNext())
                 {
                     String sortfile = (String)it.next();

                   System.out.println(sortfile);
                   
                     
                     
                 }
                    
        }
        catch(IOException ioe)
        {
            ioe.printStackTrace();
        }
    }

}
Thank you.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 23 2008
Added on Oct 17 2008
10 comments
239 views