• Post Reply Bookmark Topic Watch Topic
  • New Topic

using instance variables to sort "objects"  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

let's say we create an array of N variables that refer to objects:

Student[] students = new Student[N]

then we create the objects with a for loop (only way I know to do this)

Is there any way to sort Student[] according to one of the instance variables of the class?

the code would look like this (not complete of course but just wanted to give an idea of what I am doing if it wasn't already clear)

Class Student {

int grade1, grade2, grade3;

public static void main (String[] args){

Student[] students = new Student[N];

for (int i = 0; i < N; i++){
students[i] = new Student();

}

}
}

This code would not compile and this is not my program, I jsut want to know if, given all the instance variables have values, could I sort Student[] according to the value of the instance variable grade1 ?

thank you
[ November 25, 2006: Message edited by: Frank Ctrea ]
 
Ranch Hand
Posts: 690
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome.

I'm not sure why each student object has three grades. Wouldn't they each have one instance variable for grade? Oh, this must not be 1st grade, 2nd grade, but A, B, C...?

You could use a Collection object on which the sort() method works. Then all you would have to do to sort your collection is to implement the comparable interface in the Student class, and then you would have to define a compareTo() method for the student class so that the collection object would know how to sort students.

-- Kaydell
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In addition to Kaydell's suggestion, Collections.sort() and Arrays.sort() can also take a Comparator argument with which you can specify custom sorting criteria. For example, if your Student class looks something like this:


You can write a Comparator class that sorts by high grade:


or a Comparator that sorts by name:


Does that help at all?
 
Frank Ctrea
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kaydell Leavitt:
Welcome.

I'm not sure why each student object has three grades. Wouldn't they each have one instance variable for grade? Oh, this must not be 1st grade, 2nd grade, but A, B, C...?

You could use a Collection object on which the sort() method works. Then all you would have to do to sort your collection is to implement the comparable interface in the Student class, and then you would have to define a compareTo() method for the student class so that the collection object would know how to sort students.

-- Kaydell


Hi,

thank you for the quick reply.

This is for a homework for my programming class.

The student class has several instance variables; four different grades (howework, midterm exam, final exam, average), fist name, last name, etc.

and I want to know if there is a way to sort my objects (the table I created with the references to objects) according to one of the instance variables (average), because I have to be able to identify who had the lowest grade and who had the highest one. I could do it with a for loop comparing the values, but there must be more efficient ways to do it.

thank you

If necessary, I'll show you the real code
[ November 25, 2006: Message edited by: Frank Ctrea ]
 
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Follow up on Garrett's comparator ideas. You'll need to give Student a method that returns the value you're sorting on (average) and make a Comparator that compares two Student objects by that value.

Should student compute that average every time somebody asks for it, or store it as an instance variable? That's a decision you'll have to make. The good news is that the decision doesn't affect the Comparator at all, and you can even change your mind later without breaking the sorting stuff.
 
Frank Ctrea
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm starting to understand what I have to do.

Thank you Garrett Rowe, I'm studying your code now. It is my first programming class and I still don't know exactly what the language allows me to do. I'll study more about the comparator.
 
Frank Ctrea
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
however, can I use the java comparator for an unknown number of students? (N students and the user will provide that value)
 
Kaydell Leavitt
Ranch Hand
Posts: 690
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, do I understand correctly that implementing the Comparable interface with a compareTo() method is the usual ordering and that you can define other orderings using the Comparator interface and then defining a compare() method? I suppose that in compare(), you'd have to receive two Student objects in the two parameters as Object and then cast them back to Student, and then use any of the instance variables to order the two objects?

I'm not clear on how the compare() method works?

-- Kaydell
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kaydell Leavitt:
OK, do I understand correctly that implementing the Comparable interface with a compareTo() method is the usual ordering and that you can define other orderings using the Comparator interface and then defining a compare() method?

That's right, the Comparator is used as a callback to determine the ordering of the elements.

Originally posted by Kaydell Leavitt:
I suppose that in compare(), you'd have to receive two Student objects in the two parameters as Object and then cast them back to Student, and then use any of the instance variables to order the two objects?

If you are using Java 1.5 you can take advantage of generics, as I did with my example code, and avoid the casts alltogether. If not, you'd have to cast them as you said.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!