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