0

I want sort a list of student object containing attributes name and course such that the sorting is done based on name and if two names are same then it should consider course for sorting... I can do it separately but want a single list... PLz help...

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package studentlist;

import java.util.*;

/**
 *
 * @author Administrator
 */
public class StudentList {

    /**
     * @param args the command line arguments
     */
    String name, course;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCourse() {
        return course;
    }

    public void setCourse(String course) {
        this.course = course;
    }

    public StudentList(String name, String course) {
        this.name = name;
        this.course = course;
    }

    public static void main(String[] args) {
        // TODO code application logic here
        List<StudentList> list = new ArrayList<StudentList>();

        list.add(new StudentList("Shaggy", "mca"));
        list.add(new StudentList("Roger", "mba"));
        list.add(new StudentList("Roger", "bba"));
        list.add(new StudentList("Tommy", "ca"));
        list.add(new StudentList("Tammy", "bca"));

        Collections.sort(list, new NameComparator());
        Iterator ir = list.iterator();

        while (ir.hasNext()) {
            StudentList s = (StudentList) ir.next();
            System.out.println(s.name + " " + s.course);
        }

        System.out.println("\n\n\n ");
        Collections.sort(list, new CourseComparator());
        Iterator ir1 = list.iterator();

        while (ir1.hasNext()) {
            StudentList s = (StudentList) ir1.next();
            System.out.println(s.name + " " + s.course);
        }

    }

}

class NameComparator implements Comparator<StudentList> {

    public int compare(StudentList s1, StudentList s2) {

        return s1.name.compareTo(s2.name);

    }

}

class CourseComparator implements Comparator<StudentList> {

    public int compare(StudentList s1, StudentList s2) {
        return s1.course.compareTo(s2.course);
    }
}
1
  • you already know the condition , now apply this in your comparator class Commented Oct 14, 2013 at 7:55

3 Answers 3

6

You just have to put both the comparison in a single Comparator.

  • First test the name are equal or not:
    • If they are not equal, then return the result of comparison of name.
    • If they are equal, move to compare the courses:

Following comparator would work:

class NameCourseComparator implements Comparator<StudentList> {

    public int compare(StudentList s1, StudentList s2) {
        if (s1.name.equals(s2.name)) {
            return s1.course.compareTo(s2.course);
        }
        return s1.name.compareTo(s2.name);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

its giving the output as: Roger bba Tommy bca Tommy ca Roger mba Shaggy mca i want it as : Roger bba Roger mba Tommy bca Tommy ca Shaggy mca
@TanujJeena How are you using it? It's working fine in my case. Are you still sorting it twice? You just need one sort now, with new NameCourseComparator().
3

One simple way:

Sort the list according to the name first. After that loop through the list again. If you find any same name in order, then compare the course and update the list accordingly.

2 Comments

this is basically doing a sort twice, which isn't necessary
@Mateusz yes it is. I just suggesting a simple way which I thought of when I answered the question. Though it is inefficient, but still it answer the question correctly :)
0

You could just tweak the NameComparator a bit

class NameComparator implements Comparator<StudentList> {
      public int compare(StudentList s1, StudentList s2) {
        if(s1.getName().equals(s2.getName()) {
           return s1.getCourse().compareToIgnoreCase(s2.getCourse());
        }else {
           return s1.getName().compareToIgnoreCase(s2.getName());   
      }
}

2 Comments

you should use equals to compare string
@upog: Thanks for the correction. I've updated my answer accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.