4 Replies - 2164 Views - Last Post: 04 October 2009 - 06:37 PM Rate Topic: -----

#1 rivlinp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-October 09

How do i sort on Last Name and Date ....

Posted 04 October 2009 - 05:03 PM

I am reading data from three files and then i want to sort the records on

Gender, then Last name ascending
Last name descending
By date

Each file had three records of the following format

First Name Last Name Gender Color Date


This is the code which i have ... i can sort only on the first name using collections.sort ... how do i
sort on other columns ... any help would be appreciated ..


=======================================================================

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.*;
import java.sql.*;
import java.io.RandomAccessFile;
import java.io.*;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.lang.Object;
import java.util.Vector;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
/**
* This program reads a text file line by line and print to the console. It uses
* FileOutputStream to read the file.
*
*/





public class test1 {




public static void main(String[] args)
{
try {

ArrayList<String> rows = new ArrayList<String>();
// ArrayList<String> rows1 = new ArrayList<String>();
// ArrayList<String> rows2 = new ArrayList<String>();
// ArrayList<String> rows3 = new ArrayList<String>();
BufferedReader comma = new BufferedReader(new FileReader("C:\\comma.txt"));
BufferedReader pipe = new BufferedReader(new FileReader("C:\\pipe.txt"));
BufferedReader space = new BufferedReader(new FileReader("C:\\space.txt"));

String c= "";
String p;
String s;
while((c = comma.readLine()) != null) {


rows.add©;

}


Collections.sort(rows);
// Collections.sort(rows2);

FileWriter writer = new FileWriter("output.txt");
for(String cur: rows)
{

writer.write(cur+"");

}
// for(String cur: rows1)
// writer.write(cur+"\n");
// for(String cur: rows2)
// writer.write(cur+"\n");
comma.close();
// pipe.close();
// space.close();
writer.close();

}
catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}
}

=======================================================================

Is This A Good Question/Topic? 0
  • +

Replies To: How do i sort on Last Name and Date ....

#2 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: How do i sort on Last Name and Date ....

Posted 04 October 2009 - 05:07 PM

Please edit your post and put your code within code-tags.

Also, howcome you duplicate a lot of the import statements?
I mean, since you import this one, import java.io.*;, you don't need any import statement that says,
import java.io.[something];

Oh, and can you give a brief explanation of what your code is currently supposed to do?

This post has been edited by Gloin: 04 October 2009 - 05:10 PM

Was This Post Helpful? 0
  • +
  • -

#3 rivlinp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-October 09

Re: How do i sort on Last Name and Date ....

Posted 04 October 2009 - 05:31 PM

I am reading from three files and then i want to sort the records according to

gender, last name ascending
last name descending
by date


each file has three records in the following format :-

firstname, lastname, gender, color , date

the code which i am having i can only perform a sort on the first name using Collections.sort (), but now i want to perform a sort on gender, last name, date how do i go implementing that ....

I knw one way to do this is to use comparators but i am facing issues using them ....

the following is my code:-



import java.io.*;
import java.sql.*;
import java.io.RandomAccessFile;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.lang.Object;
import java.util.Vector;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;


public class test1 {




public static void main(String[] args)
{
try {

ArrayList<String> rows = new ArrayList<String>();

// reading data from three files
BufferedReader comma = new BufferedReader(new FileReader("C:\\comma.txt"));
BufferedReader pipe = new BufferedReader(new FileReader("C:\\pipe.txt"));
BufferedReader space = new BufferedReader(new FileReader("C:\\space.txt"));

String c= "";
String p;
String s;
while((c = comma.readLine()) != null && (p = comma.readLine()) != null && (s = comma.readLine()) != null ) {

// add records from three files into the array list

rows.add©;
rows.add(p);
rows.add(s);

}

// apply sort on the records
Collections.sort(rows);


FileWriter writer = new FileWriter("output.txt");
for(String cur: rows)
{

writer.write(cur+"");

}

comma.close();

writer.close();

}
catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}
}

This post has been edited by rivlinp: 04 October 2009 - 05:33 PM

Was This Post Helpful? 0
  • +
  • -

#4 rivlinp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-October 09

Re: How do i sort on Last Name and Date ....

Posted 04 October 2009 - 05:47 PM

Each file has 3 records of the following tpye:-


Abercrombie, Neil, Male, Tan, 2/13/1943 Bishop, Timothy, Male, Yellow, 4/23/1967 Kelly, Sue, Female, Pink, 7/12/1959
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: How do i sort on Last Name and Date ....

Posted 04 October 2009 - 06:37 PM

:code:
You will have to create a class containing the fields you want to sort on and have this call to implement Comparable
create an array of objects of that class
and then sort that array using Arrays.sort() method
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1