6 Replies - 1844 Views - Last Post: 06 September 2009 - 06:51 AM Rate Topic: -----

#1 labyrinth_fufu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 23-September 08

Sorting on More than One Fields

Posted 04 September 2009 - 11:40 PM

Hello I'm fufu :ph34r:

I need some help with my program.
I'm trying to use the class Comparable in sorting the
names of the students but it gave me quite a headache..
I'm not very familiar with it.

I'm confused which class would implement the class comparable..
How do you sort using more than one fields?

Here is the algorithm of the sorting process:
1. Sort first by last name
2. If they have the same last names, sort using their first names
3. If they have the same last names, sort using their middle names
4. If they have exactly the same name, sort by their birthdays (from youngest-to-oldest)


Here is the error produce by my program:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
No enclosing instance of type MainTestProgram is accessible. Must qualify
the allocation with an enclosing instance of type MainTestProgram
(e.g. x.new A() where x is an instance of MainTestProgram).


Can you help me modify my program?
What thing should be added to make the sorting process correct?


public class MainTestProgram {
	
		static ArrayList<Student> studentArrayList = new ArrayList<Student>();
		static int numberOfStudents;
		
		public static void main(String[] args) throws IOException {
			//some code goes here

			namesSorterX();
			
			//some code goes here
		}


		public static void namesSorterX() {	
			

			Collections.sort(studentArrayList, new StudentComparator());
			//PRODUCES THE ERROR

		}

		public class StudentComparator implements Comparator<Student>{
			public int compare(Student obj1, Student obj2){
				Student stud1 = (Student) obj1;
				Student stud2 = (Student) obj2;
				if(stud1.getBirthDate().calculateAge() < stud2.getBirthDate().calculateAge())
					return -1;
				if(stud1.getBirthDate().calculateAge() < stud2.getBirthDate().calculateAge())
					return 1;
				return 0;
			}
		}
}


public class Student extends Person implements Comparable{

	private int IDNumber;
	private Date birthDate;
	private StudentScore scores;
	
	public Student(){}

	public Student(Person name,int IDNumber, Date birthDate,StudentScore scores){}
	
	public int getIDNumber() {}

	public void setIDNumber(int number) {}
	
	public Date getBirthDate() {}
	
	public void setBirthDate(Date birthDate) {}

	public StudentScore getScores() {}

	public void setScores(StudentScore scores) {}

	public String toString(){}

	@Override
	public int compareTo(Object obj) {
		// TODO Auto-generated method stub
		Student stud = (Student) obj;
		
		int firstNameComp = getFirstName().compareToIgnoreCase(stud.getFirstName());
		
		return ((firstNameComp == 0 )?getMiddleName().compareToIgnoreCase(stud.getMiddleName()): firstNameComp);
	}
	
	public int compareLastNamesTo(Object obj) {
		// TODO Auto-generated method stub
		Student stud = (Student) obj;
		
		int lastNameComp = getLastName().compareToIgnoreCase(stud.getLastName());
		
		return (int) ((lastNameComp == 0 )? birthDate.compareTo(stud.birthDate): lastNameComp);
	}
}


public class Date implements Comparable{
	final private static int currentYear = 2009;
	private int month;
	private int day;
	private int year;

	public Date(){}
	
	public Date(int month, int day, int year){}
	
	public Date(Date birthDate){}
	
	public void setStudentBDay(int month, int day, int year){}
	
	public int getMonth() {}
	
	public void setMonth(int month) {}
		
	public int getDay() {}
	
	public void setDay(int day) {}
	
	public int getYear() {}
	
	public void setYear(int year) {}
	
	
	public int calculateAge(){
		return (currentYear - year);
	}
	
	public String toString(){}
	
	public String getModifiedBday(){}

	public int compareTo(Object obj1) {
		// TODO Auto-generated method stub
		Date stud1 = (Date) obj1;
		if(calculateAge() < stud1.calculateAge())
			return -1;
		if(calculateAge() > stud1.calculateAge())
			return 1;
		return 0;
	}
}


public class Person implements Comparable{

	private String firstName;
	private String lastName;
	private String middleName;
	
	
	public Person(){}

	public Person(String firstName, String lastName, String middleName) {}

	public Person(Person name) {}
	
	public String getFirstName() {}
	
	public void setFirstName(String firstName) {}
	
	public String getLastName() {}
	
	public void setLastName(String lastName) {}
	
	public String getMiddleName() {}

	public void setMiddleName(String middleName) {}
	
	public String getName(){
		return(lastName + " " + firstName + " " + middleName);
	}

	@Override
	public int compareTo(Object obj) {
		// TODO Auto-generated method stub
		Person emp = (Person) obj;
		int deptComp = firstName.compareToIgnoreCase(emp.getFirstName());
		return ((deptComp == 0)? lastName.compareToIgnoreCase(emp.getLastName()): deptComp);
	}
	
	public boolean equals(Object obj) {
		if(!(obj instanceof Person)){
			return false;
		}
		Person emp = (Person) obj;
		return firstName.equals(emp.getFirstName()) && lastName.equals(emp.getLastName()) && middleName.equals(emp.getMiddleName());
	}
}


public class StudentScore {
	//some method definition goes here..
}


Is This A Good Question/Topic? 0
  • +

Replies To: Sorting on More than One Fields

#2 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Sorting on More than One Fields

Posted 05 September 2009 - 12:29 AM

I am unsure if you can do that with the comparable class. I have never used it personally so I wouldn't be able to tell you, I would presumre the class that will be doing the comparing will need to implement it. As far as the 2 fields sorting I am unsure as I said. I think when it comes to that you may need to have to make your own methods on that. It would be pretty simple actually, just a series of else if statements. Are you being told to use Comparable or can you use created method? If being told to use it I can look into it real quick.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Sorting on More than One Fields

Posted 05 September 2009 - 09:48 AM

Do not sort twice
Have your compareTo() to sort on both fields

class Person implements Comparable<Person> {

	String lastName, firstName;

......
......

	// used for sorting
	public int compareTo(Person otherPerson) {
		 // first compare the last names
		 int status = lastName.equals(otherPerson.lastName));
		 // if they are NOT equals we are done comparaison by lastName is enough
		 if(status != 0)
			return status;
		 // so the lastName are equal. Return the comparaison of the first name
		 return firstName.equals(otherPerson.firstName)
	 }
}


Was This Post Helpful? 1
  • +
  • -

#4 labyrinth_fufu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 23-September 08

Re: Sorting on More than One Fields

Posted 06 September 2009 - 01:22 AM

;) I've already figured out a solution to my problem..
I made another class which implements the class comparable and it does all the sorting..

Thanks anyways guys!!

~~ PLEASE IGNORE THIS THREAD ~~
Was This Post Helpful? 0
  • +
  • -

#5 Munkon  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 61
  • Joined: 13-August 09

Re: Sorting on More than One Fields

Posted 06 September 2009 - 01:35 AM

Hi All,

I have the (almost) same problem and I didn't understand the solution..

I have a table and I want to sort the patients by urgency first and then by date.

How can I do this?

I imports ArrayList<Patients> from DB.
now, how can I sort the AL twice?

Thanks..

Munkon.
Was This Post Helpful? 0
  • +
  • -

#6 syfran  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: Sorting on More than One Fields

Posted 06 September 2009 - 01:52 AM

How bout something in the compareTo like
if this.urgency > other.urgency
	return (this is greater)
else if this.urgency == other.urgency
	// sort by date here
else if this.urgency < other.urgency
	 return (other is greater)

Was This Post Helpful? 0
  • +
  • -

#7 Munkon  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 61
  • Joined: 13-August 09

Re: Sorting on More than One Fields

Posted 06 September 2009 - 06:51 AM

Hi syfran,

Tank you..
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1