Hello,
I am a newbie to Java. I am trying to solve the following problem and been stuck from past 3-4 days. I feel I am missing something very basic and hence not able to get the answer.
Problem: Create an array of 4 Student objects. The fields are student_id, student_name,Marks. Sort the students based on student_id and based on Marks. The Arraysort method can be have this signature
void Arraysort(Student[] student_list, String sortcriteria);
Have a method int compareTo(Student Stu, String sortcrtiteria);
Can anybody explain me the way to approach the problem? I woudl like to know the body of methods Arraysort and compareTo. Should I implement and interface? ( I don't have much idead about this).
Appreciate your help.
Thanks.
Array sorting by based on different criteria
Started by heidi7, Oct 08 2009 07:33 AM
7 replies to this topic
#1
Posted 08 October 2009 - 07:33 AM
|
|
|
#2
Posted 08 October 2009 - 09:13 AM
It will depend on what Student looks like.
#3
Posted 08 October 2009 - 09:49 AM
oh! Isn't this the only way to write Student array? Are there different ways?
Student[] students ={ new Students(1, Amy, 23), new Students(2, Julian, 20)};
Hope you can help me with this.
Thanks.
Student[] students ={ new Students(1, Amy, 23), new Students(2, Julian, 20)};
Hope you can help me with this.
Thanks.
#4
Posted 08 October 2009 - 10:13 AM
My point was that we don't know what the Student class is (it could have a variety of information in it), which makes it harder to know how you might compare Student's for sorting purposes. Age, First Name, Last Name, GPA, class load, ID, etc. There are MANY sort criteria, depending on what you record. Event with Marks and ID, they could be numeric, strings, other.
#5
Posted 08 October 2009 - 10:36 AM
My Student Class
import java.util.Arrays;
public class Student
{
public static final String MARKS ="bymarks";
public static final String ID = "byid";
public int student_id;
public int marks;
public String last_name;
public String first_name;
public Student(int student_id, int marks, String last_name, String first_name)
{
this.student_id = student_id;
this.marks= marks;
this.last_name = last_name;
this.first_name = first_name;
}
import java.util.Arrays;
public class Student
{
public static final String MARKS ="bymarks";
public static final String ID = "byid";
public int student_id;
public int marks;
public String last_name;
public String first_name;
public Student(int student_id, int marks, String last_name, String first_name)
{
this.student_id = student_id;
this.marks= marks;
this.last_name = last_name;
this.first_name = first_name;
}
#7
Posted 08 October 2009 - 01:38 PM
You can make the student object implement Comparable then right it something like this:
The first if statements test for the most appropriate criteria, and you move down to the lowest level of importance.
int compareTo(student s) {
if (s.height > height) return 1;
if (s.height < height) return -1;
if (s.weight > weight) return 1;
if (s.weight < weight) return 1;
return 0;
}
The first if statements test for the most appropriate criteria, and you move down to the lowest level of importance.
#8
Posted 08 October 2009 - 05:07 PM
Thanks for the link. I will read through and give it one more try. :)
Thanks. I will give it try to solve it again.
Thanks. I will give it try to solve it again.
Edited by WingedPanther, 09 October 2009 - 07:42 AM.
Double post


Sign In
Create Account

Back to top









