I have a single demenational array with student names and a 2D array with student marks, I can sort the names fine but I cannot get the marks to match (as they have to stay in the same order). Here is my attempted code:

static String[] studentNamesArray = new String[10];
static int[][] studentMarksArray = new int[10][3];

  static void sortAlphabetical() { 
    String tempName;
    int intSwap;
    boolean flag = false;
    while (flag==false) {
      flag = true;
      for (int i = 0; i < 9; i++) {
        if (studentNamesArray[i].compareTo(studentNamesArray[i + 1])>0) {
          tempName = studentNamesArray[i];
          studentNamesArray[i] = studentNamesArray[i + 1];
          studentNamesArray[i + 1] = tempName;

          for(int y=0;y<2;y++){
          intSwap = studentMarksArray[i][0];
          studentMarksArray[i][y] = studentMarksArray[i+1][y+1];
          studentMarksArray[i+1][y+1] = intSwap;
          }
          flag = false;
        }
      }
    }
  }
share|improve this question
1  
Any reasons that you don't want to create a Student object holding its name and its marks, then create a Student[] array and sort it by name using a Comparator ? – Alexis C. Nov 28 '13 at 15:58
    
I'm limited to non OO methods – ColinShewell Nov 28 '13 at 15:59
    
compareTo is a OO method... – brimborium Nov 28 '13 at 16:07
    
What I meant was I could not have have separate class files – ColinShewell Nov 28 '13 at 16:11
up vote 3 down vote accepted

You just have to swap the marks arrays like you did for the names :

static void sortAlphabetical() { 
        String tempName;
        int [] intSwap; //<-- note I changed this as an int[] array
        boolean flag = false;
        while (flag==false) {
          flag = true;
          for (int i = 0; i < studentNamesArray.length-1; i++) { //<-- note I changed this to length - 1 to avoid IndexOutOfBoundsException
            if (studentNamesArray[i].compareTo(studentNamesArray[i + 1])>0) {
              tempName = studentNamesArray[i];
              studentNamesArray[i] = studentNamesArray[i + 1];
              studentNamesArray[i + 1] = tempName;

              intSwap = studentMarksArray[i];
              studentMarksArray[i] = studentMarksArray[i+1];
              studentMarksArray[i+1]= intSwap;
              flag = false;
            }
          }
        }
      }

    static String[] studentNamesArray = new String[3];
    static int[][] studentMarksArray = new int[3][3];

    public static void main (String[] args) throws java.lang.Exception {
        studentNamesArray[0] = "Mark";
        studentNamesArray[1] = "Anna";
        studentNamesArray[2] = "Arnold";

        studentMarksArray[0] = new int[]{1,2,3};
        studentMarksArray[1] = new int[]{4,5,6};
        studentMarksArray[2] = new int[]{0,0,0};

        sortAlphabetical();
        System.out.println(Arrays.toString(studentNamesArray));
        System.out.println(Arrays.deepToString(studentMarksArray));

    }

Output :

[Anna, Arnold, Mark]
[[4, 5, 6], [0, 0, 0], [1, 2, 3]]
share|improve this answer
    
Can you explain why you changed intSwap to an array? – ColinShewell Nov 28 '13 at 16:07
1  
@Colin747 Because initially each mark array is associated as the same index position as the String array right ? So if you swap two names in the array, you have too swap their corresponding mark arrays too. – Alexis C. Nov 28 '13 at 16:08
    
I wasn't questioning why you did it, I just wanted to make sure I understood correctly what was going on, your solution worked great, thanks – ColinShewell Nov 28 '13 at 16:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.