0

First of all, in this post I'm not asking a question. I'm posting this because I need suggestions from your side.

I'm working in Struts MVC and this was my requirement :-

There is a page for Class entry, where user adds, edits and deletes classes for students in a college. So after the above three process, when page is refreshed, the data which are present in the table should be displayed in the rest of the page area. That list will show SlNo., Course name, Semester, Class name, Class code etc.

I'm using a TO class to show the existing data in the page. Earlier the list was sorted based on Course name [TO was Comparable type, mechanism for comparison was based on Course name]. Now they want the sorting based on Course name and Semester number, that means the list should come like

  1. BA Economic 1
  2. BA Economic 2
  3. BA Economic 3
  4. BA Economic 4
  5. BA Economic 5
  6. BA Economic 6
  7. BA English 1
  8. BA English 2
  9. BA English 3
  10. BA English 4
  11. BA English 5
  12. BA English 6

So initially I changed mysql query to give order by course name, semester. Then I changed the TO class so that it doesn't have an inbuilt comparison mechanism. The code worked fine.

But after some time I got another idea, I tried to implement the same mechanism using Comparable interface, where in the comapreTo method I took the hashcodes of both course name and semester of current reference and added them, then I did the same thing with the argument reference of comapreTo method and I took the difference of both the answers and returned it. Surprisingly it worked, but I'm not sure will this be working in every scenario. Is this a good way of meeting a requirement? Is this a good way of programming?

Sample code :-

package com.sample;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

class Classes implements Comparable<Classes> {

    private String name;
    private int semester;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getSemester() {
        return semester;
    }
    public void setSemester(int semester) {
        this.semester = semester;
    }   
    public Classes(String name, int semester) {
        super();
        this.name = name;
        this.semester = semester;
    }
    public int compareTo(Classes other) {
        return (this.name.hashCode() + new Integer(this.semester).hashCode()) - 
               (other.name.hashCode() + new Integer(other.semester).hashCode());
    }
}

public class ComparableMultipleAttributes {

    public static void main(String[] args) {

        List<Classes> classList = new ArrayList<Classes>();
        classList.add(new Classes("BA English", 1));
        classList.add(new Classes("BA Economics", 3));
        classList.add(new Classes("BA Economics", 1));
        classList.add(new Classes("BA English", 6));
        classList.add(new Classes("BA English", 5));
        classList.add(new Classes("BA Economics", 2));
        classList.add(new Classes("BA Economics", 5));
        classList.add(new Classes("BA English", 4));
        classList.add(new Classes("BA Economics", 4));
        classList.add(new Classes("BA English", 3));
        classList.add(new Classes("BA Economics", 6));
        classList.add(new Classes("BA English", 2));
        Collections.sort(classList);

        Iterator<Classes> it = classList.iterator();
        while(it.hasNext()) {

            Classes c = it.next();
            System.out.println(c.getName() + "---" + c.getSemester());
            System.out.println();
        }
    }
}
Arun Sudhakaran
  • 2,167
  • 4
  • 27
  • 52
  • You really really should NOT name anything `Class` –  Apr 04 '17 at 09:19
  • K, actually in the code it is Classes – Arun Sudhakaran Apr 04 '17 at 09:23
  • see http://stackoverflow.com/questions/369512/how-to-compare-objects-by-multiple-fields –  Apr 04 '17 at 09:23
  • Using `hashCode` like this is not recommended. The order will be based on the `hashCode`, which is pretty much undefined. – john16384 Apr 04 '17 at 09:30
  • but hashcode is generated based on the value of that object right? Here I'm getting the hashcode of String and Integer, not the object of TO. – Arun Sudhakaran Apr 04 '17 at 09:32
  • If `string1.hashCode() > string2.hashCode()` that doesn't mean that `string1 > string2`. It pretty much never is. – M. Prokhorov Apr 04 '17 at 09:38
  • Also, sorting in memory when you can sort in database is pretty much never the right answer, since sorting in memory is usually much slower and doesn't respect request pagination (even if it does respect pagination, it causes way more data to be read than needed). – M. Prokhorov Apr 04 '17 at 09:41

1 Answers1

1

Besides the good comments that you got, if you really want a comparator to sort by multiple properties, then using jdk-8 is pretty easy to achieve:

     Comparator.comparing(Classes::getName)
       .thenComparing(Classes::getSemester)
       ... other properties
Eugene
  • 117,005
  • 15
  • 201
  • 306