-4

The task was to create a program which organises a users name with their corresponding mark and displays this in descending order. I'm asking how to sort both arrays at the same time but only by comparing the elements of one of the arrays. As a restriction, I cannot use classes, only associated arrays.

int [] ArrMarks = new int [5];
String [] ArrNames = new String [5];

Button to Accept user input

for (int i = 0; i < 5; i++)
{
    ArrNames[i] = JOptionPane.showInputDialog("Enter a Name:");
    ArrMarks[i] = Integer.parseInt(JOptionPane.showInputDialog("Enter a mark:")); 
}

Display button including the part which sorts the code, this is the main part I am unsure of.

int Hi = ArrMarks[0];
for(int i = 0; i < 5; i++) {
    if(ArrMarks[i] > Hi) {
        Hi = ArrMarks[i];
    }
}

txaDisplay.append("Names:"+"\t\t"+"Marks");
for (int i = 0; i < 5; i++) {
    txaDisplay.append(ArrNames[i]+"\t\t"+ArrMarks[i]+"\n");
}
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
rubin.kazan
  • 131
  • 12

2 Answers2

2

First of all, I would like to give a suggestion. Use lists instead of arrays. This kind of coding is not a good conventional way of coding. I will show a conventional way to handle the issue. You may take this if you like.

Create another class Mark.java with the following content

class Mark {
    Integer mark;
    String name;

    public Integer getMark() {
        return this.mark
    }
    public void setMark(Integer mark) {
        this.mark = mark
    }
    public String getName() {
        return this.name
    }
    public void setName(String name) {
        this.name = name
    }
}

Then in your current class create a List of this class.

List<Mark> marks = new ArrayList<Mark>();

Then after reading content from the user, you can add them to the list like following

for (int i = 0; i < 5; i++)
{
    Mark mark = new Mark();
    String name = JOptionPane.showInputDialog("Name:");
    Integer mark = Integer.parseInt(JOptionPane.showInputDialog("Mark:")); 
    marks.add(mark)
}

Write a comparator class named MarkComparator.java. It deals with the sorting part.

public class MarkComparator implements Comparator<Mark> {
    @Override
    public int compare(Mark m1, Mark m2) {
        return m1.getMark().compareTo(m2.getMark());
    }
}

The use

Collections.sort(marks, new MarkComparator());

Now the List<Mark> of marks will be sorted.

You may then view the list the same way.

txaDisplay.append("Names:"+"\t\t"+"Marks");
for (Mark mark : marks)
{
    txaDisplay.append(mark.getName()+"\t\t"+mark.getMark()+"\n");
}
Unihedron
  • 10,902
  • 13
  • 62
  • 72
Philip John
  • 5,275
  • 10
  • 43
  • 68
1

The solution to my question required using a sorting technique to sort the names and marks, so that the program prints the corresponding mark to the name.

for(int i = 0; i < 5-1; i ++)
{
    for(int j = i +1; j < 5; j++)
{
 if (ArrMarks[i] < ArrMarks[j]) 

 {
    //sorting the names
    String temp = ArrNames[i];
    ArrNames[i] = ArrNames[j];
    ArrNames[j] = temp;

    //sorting the marks
    int temp1 = ArrMarks[i];
    ArrMarks[i] = ArrMarks[j];
    ArrMarks[j] = temp1;  

}  

}   
txaDisplay.append("Names:"+"\t\t"+"Marks"+"\n");

        txaDisplay.append(ArrNames[i]+"\t\t"+ArrMarks[i]+"\n"); 
}
rubin.kazan
  • 131
  • 12