Re: Sorting on Multiple Columns
From: xarax (xarax_at_email.com)
Date: 01/19/05
- Next message: dhiraj.peechara_at_gmail.com: "Re: Desktop Sharing help"
- Previous message: Andrew McDonagh: "Re: A class that uses instances of itself? Is this right?"
- In reply to: jr: "Sorting on Multiple Columns"
- Next in thread: jr: "Re: Sorting on Multiple Columns"
- Reply: jr: "Re: Sorting on Multiple Columns"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 18 Jan 2005 23:20:50 GMT
"jr" <jrolandumuc@yahoo.com> wrote in message
news:1106088810.003566.6050@z14g2000cwz.googlegroups.com...
> Hi. I'm fairly new to Java and just tried for the first time today to
> sort a data set by multiple columns. I found many solutions for
> sorting on a single column, but none on how to sort on multiple columns
> (none that made sense to my tragically small understanding of Java
> programming, anyway). I think I may have finally found a solution, but
> it may be highly inefficient, full of pitfalls, etc. I'd appreciate
> any advice concerning my simple below, and if this approach is at all
> correct.
>
> The goal of the classes is simply to create a few student objects, and
> then sort them by student id, first name, middle initial, and then last
> name.
>
> Thanks!
>
/snip/
/***************** Sortable Student Class ***************/
import java.util.*;
public class Student
implements Comparable
{
private static class StudentComparator
implements Comparator
{
public int compare(final Object obj_1, final Object obj_2)
{
final Student student_1;
final Student student_2;
student_1 = (Student) obj_1;
student_2 = (Student) obj_2;
return student_1.compareTo(student_2);
}
}
public static final Comparator STUDENT_COMPARATOR = new StudentComparator();
/* Instance variables */
public final int student_id;
public final String last_name;
public final String mi;
public final String first_name;
/* Object constructor */
public Student(final int theStudentId, final String theFirstName, final
String theMi, final String theLastName)
{
super();
student_id = theStudentId;
first_name = theFirstName;
mi = theMi;
last_name = theLastName;
}
public int compareTo(final Student theStudent)
{
final int theId;
final String theFirstName;
final String theMi;
final String theLastName;
int result;
theId = theStudent.student_id;
theFirstName = theStudent.first_name;
theMi = theStudent.mi;
theLastName = theStudent.last_name;
result = (student_id == theStudent_id) ? 0 :
((student_id < theStudent_id) ? -1 : 1);
if(0 == result)
{
result = last_name.compareToIgnoreCase(theLastName);
if(0 == result)
{
result = mi.compareToIgnoreCase(theMi);
if(0 == result)
{
result = first_name.compareToIgnoreCase(theFirstName);
}
}
}
return result;
}
public int compareTo(final Object theObject)
{
final Student student;
student = (Student) theObject;
return compareTo(student);
}
}
/**************************************************/
/***************** Caller Program ***************/
import java.util.*;
public class TryStudent
{
public static void main(String[] args)
{
/* Create an array of Student objects */
Student[] s = new Student[5];
s[0] = new Student(1,"Joe", "I.","Richards");
s[1] = new Student(1,"Joe", "I.","Mitchell");
s[2] = new Student(3,"John","A.","Adams");
s[3] = new Student(2,"Jake","R.","Scott");
s[4] = new Student(1,"Jack","B.","Nimble");
/* Sort array */
Arrays.sort(s,Student.STUDENT_COMPARATOR);
/* Print out sorted values */
for(int i = 0; i < s.length; i++)
{
System.out.println(s[i].student_id + " " +
s[i].first_name + " " +
s[i].mi + " " +
s[i].last_name);
}
}
}
/*********************************************************/
- Next message: dhiraj.peechara_at_gmail.com: "Re: Desktop Sharing help"
- Previous message: Andrew McDonagh: "Re: A class that uses instances of itself? Is this right?"
- In reply to: jr: "Sorting on Multiple Columns"
- Next in thread: jr: "Re: Sorting on Multiple Columns"
- Reply: jr: "Re: Sorting on Multiple Columns"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|