9 Replies Latest reply: Apr 3, 2007 5:40 PM by 807599 RSS

    Arrays.sort??????

    807599
      Heres my problem,How do I sort two arrays that are to match each other?
      Example.
      One guys name is Bob he is 20 stored in 0
      Anoter guys name is Joe he is 40 stored in 1
      Another guys name is Matt he is 30 stored in 2

      how do I have it print out this
      Bob 20
      Matt 30
      Joe 40

      and not this
      Bob 20
      Joe 30
      Matt 40

      I do the Arrays.sort but it sorts tha numbers and tha mix
        • 1. Re: Arrays.sort??????
          807599
          Sort an array or Collection of Person objects, where a person has a name and an age.
          You will want to supply a comparator to determine the ordering:

          http://java.sun.com/docs/books/tutorial/collections/algorithms/index.html
          • 2. Re: Arrays.sort??????
            807599
            I read it and it didn't make sense but I'll look it up in a book of mine maby that could shed some light on it.
            • 3. Re: Arrays.sort??????
              807599
              Are you using parrellel arrays or is it one array of an object? If it's an object, you need to (I think) implement comparable and override compare(Object o). Else you cannot use Array.Sort on parellel arrays.
              • 4. Re: Arrays.sort??????
                807599
                If you want to sort parallel arrays, you have to implement sorting yourself. Here's a recent thread describing how to Do It Right?
                http://forum.java.sun.com/thread.jspa?threadID=5156351
                • 5. Re: Arrays.sort??????
                  807599
                  No,I know how to sort (no shure what parrell arrays are)but I want to sort them in a group(by age but keeping thier name and age with each other like a pair).
                  • 6. Re: Arrays.sort??????
                    807599
                    parrellel arrays are arrays in which you have more than one array with related data, like age and name. Personally, I stay away from them and make an object and do it that way.
                    • 7. Re: Arrays.sort??????
                      800282
                      Are you using parrellel arrays or is it one array of
                      an object? If it's an object, you need to (I think)
                      implement comparable and override compare(Object o).
                      No, it's the compareTo(Object) method with the java.lang.Comparable interface.
                      It's the compare(Object, Objec) method if the OP want to write his own implementation of java.util.Comparator interface.

                      ... Sort on parellel arrays.
                      That wouldn't be wise.
                      • 8. Re: Arrays.sort??????
                        807599
                        The better way to do this is to create a class to encapsulate the data from those arrays.
                        class Person {
                            String name;
                            int age;
                        }
                        Then you only have one array that holds Person objects.
                        Person[] peopleList = new Person[3];
                        Then either implement Comparable or Comparator to use one of the Collections.sort methods.
                        • 9. Re: Arrays.sort??????
                          807599
                          I don't recommend parallel arrays. You should heed prior advice to create a class to model the data in the arrays.

                          But if you simple must have parallel arrays, you need to make sure you swap elements in each arrays at the same time, inside on sorting pass.

                          Here's some bad code that shows what I mean:
                          public class Test {
                              String[] names = new String[3];
                              int[] ages = new int[3];
                              
                              public void Test() {
                                  names[0] = "Bob";
                                  ages[0] = 20;
                                  names[1] = "Joe";
                                  ages[1] = 40;
                                  names[2] = "Matt";
                                  ages[2] = 30;
                              }
                          
                              public void sortByAge() {
                                  for (int i = 0; i < ages.length; i++) {
                                      for (int j = i; j < ages.length; j++) {
                                          if (ages[i] > ages[j]) {
                                              int tempInt = ages;
                          ages[i] = ages[j];
                          ages[j] = tempInt;
                          String tempString = names[i];
                          names[i] = names[j];
                          names[j] = tempString;
                          }
                          }
                          }

                          }

                          public static void main(String[] args) {
                          Test test = new Test();

                          System.out.println("ages: " + test.ages[0] + ", " + test.ages[1] + ", " + test.ages[2]);
                          System.out.println("names: " + test.names[0] + ", " + test.names[1] + ", " + test.names[2]);

                          test.sortByAge();

                          System.out.println("ages: " + test.ages[0] + ", " + test.ages[1] + ", " + test.ages[2]);
                          System.out.println("names: " + test.names[0] + ", " + test.names[1] + ", " + test.names[2]);
                          }
                          }