sorting 3 dirernt arrays
i need to sort one array but i need the other arrays that corospond with that array here is the code so far
publicstaticvoid main(String[] args)
{
String name[]=new String[20];
int age[]=newint[20];
float gpa[]=newfloat[20];
String output[]=new String[20];
float tmp;
for(int i=0;i<name.length;i++)
{
name[i]=JOptionPane.showInputDialog("enter a name");
age[i]=Integer.parseInt(JOptionPane.showInputDialog("enter their age"));
gpa[i]=Float.parseFloat(JOptionPane.showInputDialog("enter their gpa"));
}
for (int x=0;x<20;x++)
{
for (int i=0;i<20;i++)
{
if (gpa[i+1]><gpa[i])
{
tmp=gpa[i];
gpa[i]=gpa[i+1];
gpa[i+1]=tmp;
}
}
}
}
the problem is i can't figure out how to get name and age to corospond with the gpa>
[1867 byte] By [
smithboba] at [2007-10-2 6:55:25]

> i need to sort one array but i need the other arrays
> that corospond with that array here is the code so
> far
Ok, stop before it's too late! ; )
You really dont want to have three different arrays. Create a Student object which holds those variables for you and then use a get-method to sort your array instead.
Here's an example:
// Student.java
public class Student {
private String name;
private int age;
private float gpa;
public Student(String n, int a, float g) {
name = n;
age = a;
gpa = g;
}
public float getGPA() {
return gpa;
}
public String toString() {
return "[name="+name+", age="+age+", gpa="+gpa+"]";
}
}
And a class to test it:
// TestStudent.java
import javax.swing.JOptionPane;
class TestStudent {
public static void main(String[] args) {
Student[] students = new Student[3];
for(int i = 0; i < students.length; i++) {
String name = JOptionPane.showInputDialog("enter a name");
int age = Integer.parseInt(JOptionPane.showInputDialog("enter their age"));
float gpa = Float.parseFloat(JOptionPane.showInputDialog("enter their gpa"));
students[i] = new Student(name, age, gpa);
}
// Sort your array here
for(int i = 0; i < students.length; i++) {
System.out.println(students[i]);
}
}
}
If you have any further questions about this, please post them in the fundamentals forum:
http://forum.java.sun.com/category.jspa?categoryID=5
Good luck.
More ... you can define a Comparator
public class StudentGPAComparator implements Comparator<Student> {
public int compare(Student a, Student b) {
return a.getGPA() - b.getGPA();
}
}
... and you can store Student's in a TreeSet<Student> using the previously defined Comparator and the objects will be automatically sorted in the collection.
> More ... you can define a Comparator
That, of coure, is much better. But seeing how the OP started his assignment, I got the ipmression that s/he is just starting to program. And before using some built-in sorting method and datastructure, it's a good practice to sort it yourself and use a simple array for it.
import java.util.ArrayList;
class Student{
int age;
String name;
int gpa;
public Student(int age,String name,int gpa){
this.age=age;
this.name=name;
this.gpa=12;
}
}
public class Sample{
public static void main(String[] args){
Student stdnt[]=new Student[3];
stdnt[0]=new Student(10,"RAM",3);
stdnt[1]=new Student(7,"MUTHY",66);
stdnt[2]=new Student(9,"BABU",4);
sort(stdnt);
System.out.println(stdnt[0].age);
System.out.println(stdnt[1].age);
System.out.println(stdnt[2].age);
}
public static void sort(Student stdnt[]){
int size=stdnt.length,i=0,j=0;
Student temp;
for(i=0;i<size;i++){
for(j=i+1;j<size;j++){
if(stdnt.age>stdnt[j].age){
temp=stdnt;
stdnt=stdnt[j];
stdnt[j]=temp;
}
}
}
}
}
Hello I was wondering how you use the getGPA function and actually sort it... Could you give a little more example?Thank you