0

I have to sort a database of 1 string array and 2 int arrays. This is what I have so far:

public static void sortDatabase(int numRecords, String[] sDeptArr, 
              int[] iCourseNumArr, int[] iEnrollmentArr)
   {
       int length = sDeptArr.length;
       for(int i=0; i<length-1; i++)
       {
           int iPosMin = i;
           for(int j=i+1; j<length; j++)
           {
               if(sDeptArr[j].compareTo(sDeptArr[iPosMin]) == 0)
                   iPosMin = j;
               else if(sDeptArr[j].equals(sDeptArr[iPosMin]) && iCourseNumArr[j] < iCourseNumArr[iPosMin])
                   iPosMin = j;
           }
       }
   }

I have yet to test it because the entire program is not done but does this look like it is going in the right direction? I want to sort the database in alphabetical order by name first, then if the names are the same, use the course number to sort.

2
  • 1
    Any reasons, you aren't using sql to do it? Commented Nov 27, 2010 at 18:36
  • Because I have to do it in java. Commented Nov 27, 2010 at 18:51

1 Answer 1

2

IMHO your direction is not optimal. The best way I know is to create new data structure

public class Data implements Comparable<Data> {
    private String sDeptArr;
    private int iCourseNumArr;
    private int iEnrollmentArr;

    public int compareTo(Data other) {
    // your implementation
    }
}

Now create an array or collection of Data:

List<Data>
Data[]

Now use Arrays.sort() or Collections.sort().

Sign up to request clarification or add additional context in comments.

1 Comment

Not that simple. We have to use java and do it the way that the professor said using a method and using something like that seen above in my original post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.