Re: Sorting on Multiple Columns

From: xarax (xarax_at_email.com)
Date: 01/19/05


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



Relevant Pages

  • Re: Sorting on Multiple Columns
    ... > public class Student ... > public Student(final int theStudentId, final String theFirstName, ... > public class TryStudent ...
    (comp.lang.java.programmer)
  • Re: Alphabetical sorting in VB seems to be different in listboxes and in Access
    ... But when I used a sorted listbox in VB dot net, ... Is the VB sort order changeable? ... Public Class MyListBox ... For Each item As String In Me.Items ...
    (microsoft.public.dotnet.languages.vb)
  • Re: VB6 LISTBOX problem
    ... Have a look at the code below, can you do a sort that is faster ... There are of course all sorts of ways of sorting stuff, especially stuf that is "linked together" as in your UDTs, but to prove the following code sorts it in exactly the same way that your own original code does so, by concatenating all three items of each element into a composite string exactly as you are already doing, but instead of dumping those composite strings into a ListBox it dumps them into a VB string array. ... Private Type SAFEARRAY1D ... Dim StPtr As Long, VAs String, pVAs Long ...
    (microsoft.public.vb.general.discussion)
  • C Sharp sorting considered superior to C by an order of magnitude
    ... following sort algorithms and the following points. ... its own comparision for the types bool, byte, short, int, long, ... single, double and string. ... private CheckBox CHKdeterminism; ...
    (comp.programming)
  • Re: Cant Figure Out How To Sort On Bind
    ... The sort order is not being passed as an argument, ... with the string "searchCache" to create a key for it, ... 'Assign ColumnOrder to ViewState ... Sub GetDataReader(ByVal ColumnOrder As String) ...
    (microsoft.public.dotnet.framework.aspnet)