ClassApe ClassApe - 4 years ago 176
Java Question

How to sort object array in ascending order based on instance variables using the following code

im trying to sort this object array without comparator or array list. sorting int the array for average score. here how it is used in the main method if someone could give me an idea that would be helpful i really dont want to use an array list or comparator, so i guess i need algorithm to help me, but im struggling figuring one out :

public void calcAvgScore()
{
avgScore = (test1 + test2) / 2;
}


then in the constructor is:

public Student(String newName, int newId, int newTest1, int newTest2)
{
super(newName, newId);
test1 = newTest1;
test2 = newTest2;
calcAvgScore();
calcGrade();
}


Then:

public static void main(String[] args)
{
Student[] score = new Student[5];

Scanner keyboard = new Scanner(System.in);

System.out.println("Please enter your students first name, id number, and two test scores");
String newName = keyboard.next();
int newId = keyboard.nextInt();
int newTest1 = keyboard.nextInt();
int newTest2 = keyboard.nextInt();

Student student1 = new Student(newName, newId, newTest1, newTest2);

System.out.println("Please enter your second students first, id number and two test scores");
String newName2 = keyboard.next();
int newId2 = keyboard.nextInt();
int newTest12 = keyboard.nextInt();
int newTest22= keyboard.nextInt();

Student student2 = new Student(newName2, newId2, newTest12, newTest22);

System.out.println("Please enter your third students first name, id number, and two test scores");
String newName3 = keyboard.next();
int newId3 = keyboard.nextInt();
int newTest13 = keyboard.nextInt();
int newTest23= keyboard.nextInt();

Student student3 = new Student(newName3, newId3, newTest13, newTest23);

System.out.println("Please enter your fourth students first name, id number, and two test scores");
String newName4 = keyboard.next();
int newId4 = keyboard.nextInt();
int newTest14 = keyboard.nextInt();
int newTest24= keyboard.nextInt();

Student student4 = new Student(newName4, newId4, newTest14, newTest24);

System.out.println("Please enter your fifth students first name, id number, and two test scores");
String newName5 = keyboard.next();
int newId5 = keyboard.nextInt();
int newTest15 = keyboard.nextInt();
int newTest25= keyboard.nextInt();

Student student5 = new Student(newName5, newId5, newTest15, newTest25);

score[0] = student1;
score[1] = student2;
score[2] = student3;
score[3] = student4;
score[4] = student5;
System.out.println("____________");

max = score[0];
int i;
for(i =0; i < score.length; i++)
{

System.out.println(score[i]);

System.out.println("____________");
}

}


}

Answer Source

It's simply like this: you can sort without Comparator, instead you implement it yourself. In another word, actually you cannot. Algorithm cannot help you, as far as I see. If you don't have the average score, how you fill the array to sort?

  1. Save the average grade in your Student class, as you did. (although I think you may have to change the constructor. It's better to provide the Student class's details.)
  2. You sort with student.avgScore.

Like:

max = score[0];
int x = 0;
Student temp = null;
for (int i=0; i < score.length; i++) {
    x = score[i].avgScore;
    temp = score[i];
    if (x < score[i+1].avgScore ) {
        score[i] = score[i+1];
        score[i+1] = temp;
    }
}

Just a rough idea. Algorithm speeds up the sorting, but you sort with actual data.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download