Sorting on Multiple Columns

Discussion in 'Java' started by jr, Jan 18, 2005.

  1. jr

    jr Guest

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

    }
    /*********************************************************/
     
    jr, Jan 18, 2005
    #1
    1. Advertising

  2. jr

    xarax Guest

    "jr" <> wrote in message
    news:...
    > 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);
    }
    }
    }
    /*********************************************************/
     
    xarax, Jan 18, 2005
    #2
    1. Advertising

  3. jr

    jr Guest

    xarax wrote:
    >
    > /***************** 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 ;-)
     
    jr, Jan 19, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. helpful sql
    Replies:
    0
    Views:
    824
    helpful sql
    May 19, 2005
  2. Replies:
    5
    Views:
    607
  3. Replies:
    4
    Views:
    942
  4. Tom Kirchner

    sorting by multiple criterias (sub-sorting)

    Tom Kirchner, Oct 11, 2003, in forum: Perl Misc
    Replies:
    3
    Views:
    478
    Michael Budash
    Oct 11, 2003
  5. Replies:
    3
    Views:
    374
    Tad McClellan
    Nov 9, 2006
Loading...

Share This Page