Sorting on Multiple Columns

J

jr

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!


/***************** Sortable Student Class ***************/
import java.util.*;

public class Student implements Comparable {

// Instance variables
public int student_id;
public String last_name, mi, first_name;

// Object constructor
public Student(int student_id,
String first_name,
String mi,
String last_name) {
this.student_id = student_id;
this.first_name = first_name;
this.mi = mi;
this.last_name = last_name;
}

// Overload compareTo method
public int compareTo(Object student) {
Student tmp = (Student)student;
if (this.student_id != tmp.student_id) {
// First compare on ids
if (this.student_id < tmp.student_id) return -1;
if (this.student_id > tmp.student_id) return 1;
return 0;
}
else if (this.first_name != tmp.first_name) {
// Next, compare on the first name
int i = (this.first_name).compareTo(tmp.first_name);
if (i < 0) return -1;
if (i > 1) return 1;
return 0;
}
else if (this.mi != tmp.mi) {
// Next, compare on the middle initial
int i = (this.mi).compareTo(tmp.mi);
if (i < 0) return -1;
if (i > 1) return 1;
return 0;
}
else if (this.last_name != tmp.last_name) {
// Finally, compare on the last name
int i = (this.last_name).compareTo(tmp.last_name);
if (i < 0) return -1;
if (i > 1) return 1;
return 0;
}
else {
// Nothing else to compare, just return 0
return 0;
}
}

}

/**************************************************/


/***************** 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);

// Print out sorted values
for(int i = 0; i < s.length; i++) {
System.out.println(s.student_id + " " +
s.first_name + " " +
s.mi + " " +
s.last_name);
}
}

}
/*********************************************************/
 
X

xarax

jr said:
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.student_id + " " +
s.first_name + " " +
s.mi + " " +
s.last_name);
}
}
}
/*********************************************************/
 
J

jr

xarax said:
/***************** 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.student_id + " " +
s.first_name + " " +
s.mi + " " +
s.last_name);
}
}
}
/*********************************************************/


Thanks! I can see I have a lot of work to do to understand Java
sorting, but this is a great start ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top