A method to sort a parallel array(name,mark,age),sort according to name array?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pennyshidute
    New Member
    • Sep 2011
    • 1

    A method to sort a parallel array(name,mark,age),sort according to name array?

    How do i Write a method to sort the parallel arrays according to the ascending order of names, if the two other arrays are numeric and not string like the name array?

    Code:
    public void sortD(String[]name,int[] age, double[] mark) 
        {
            for (int i = 0; i < age.length - 1; i++) 
            {
                for (int j = 0; j < age.length -1-i; j++) 
                {
                    if (age[j] > age[j + 1]) 
                    {
            /*
            *Swap age with lowest age
            */
                    int ageTemp = age[j];
                            age[j] = age[j + 1];
                                  age[j + 1] = ageTemp;
                                        
                 /*
                   Swap name using the same index of the age
                           */
    
                            String nameTemp = name[j];
                               name[j] = name[j + 1];
                                 name[j + 1] = nameTemp;
                                    
                    double markTemp=mark[j];
                      mark[j]= mark[j + 1];
                       mark[j + 1]=markTemp;                                               
                    }
                                    
               }
    Last edited by Niheel; Mar 27 '13, 07:27 AM.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    I don't know what you question is. Is the code throwing an error? What is the error message? Is it not working the way you expect? What do you expect? What is it doing instead?

    Comment

    • chaarmann
      Recognized Expert Contributor
      • Nov 2007
      • 785

      #3
      You have this line
      Code:
      if (age[j] > age[j + 1])
      This determines the order of the data in your bubble-sort like algorithm.
      Just exchange it with
      Code:
      if (name[j].compareTo(name[j + 1]) > 0)
      and it sorts it by name ascending instead.

      If you want a high code quality, use object orientation, that means don't use parallel arrays to store the data, instead create a class Person and store your data in a single array of this class (or even better, in a List instead of in array), using Getter/Setters or fields of this class.
      Code:
      // keep all data together in a single object
      class Person {
          public String name;
          public int age;
      
          // this method is for nice printing
          public String toString() {
              return "name: " + name + " age: " + age;
          }
      }
      
      // declare storage for many object instances
      List personList = new ArrayList();
      
      // setup demo data
      String[] name = {"B-name", "A-name"};
      int[] age = {42, 18};
      
      // store data
      for (int i = 0, length = name.length; i < length; i++) {
          Person p = new Person();
          p.name = name[i];
          p.age = age[i];    
          personList.add(p);
      }

      And second, don't reinvent sorting algrithms but use Collections.sor t()
      Code:
      // sort by name
      class NameComparator implements Comparator { 
          public int compare(Object p1, Object p2) {
              return ((Person)p1).name.compareTo(((Person)p2).name); 
          } 
      }
      Collections.sort(personList, new NameComparator());
      
      // print results
      System.out.println("sorted person array=" + personList);
      So if you run these two code snippets, it will print out:
      Code:
      sorted person array=[name: A-name age: 18, name: B-name age: 42]
      If you need more fields like "mark", "addresss" etc, put them into your Person class. If you need to sort by them, write a Comparator for each of them, for example MarkComparator, AddressComparat or etc., and sort them by using
      Code:
      Collections.sort(personList, new MarkComparator());
      Collections.sort(personList, new AddressComparator());
      You can even use anonymous inner classes for that, for example you do not declare a AgeComparator, but sort directly this way:
      Code:
      Collections.sort(personList, new Comparator() { 
          public int compare(Object p1, Object p2) {
              int age1 = ((Person)p1).age;
      		int age2 = ((Person)p2).age;
              return (age1 > age2 ? 1 : (age1 == age2 ? 0 : -1)); 
          } 
      });
      You can even improve more, for example by defining a constructor for your class Person that takes as arguments all the data beloging to one person (Age, Name, etc.). Or if you don't read in arrays with the name, age etc. in the first way and then later assign them to a person list in a separate for-loop, but assign the data directly to a person and then put him in the list. And so on.

      Comment

      Working...