455,806 Members | 1,363 Online
Need help? Post your question and get tips & solutions from a community of 455,806 IT Pros & Developers. It's quick & easy.

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

 P: 1 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? Expand|Select|Wrap|Line Numbers   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;                                                                }              }   Mar 26 '13 #1

You have this line
Expand|Select|Wrap|Line Numbers
1. if (age[j] > age[j + 1])
This determines the order of the data in your bubble-sort like algorithm.
Just exchange it with
Expand|Select|Wrap|Line Numbers
1. 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.
Expand|Select|Wrap|Line Numbers
1. // keep all data together in a single object
2. class Person {
3.     public String name;
4.     public int age;
5.
6.     // this method is for nice printing
7.     public String toString() {
8.         return "name: " + name + " age: " + age;
9.     }
10. }
11.
12. // declare storage for many object instances
13. List personList = new ArrayList();
14.
15. // setup demo data
16. String[] name = {"B-name", "A-name"};
17. int[] age = {42, 18};
18.
19. // store data
20. for (int i = 0, length = name.length; i < length; i++) {
21.     Person p = new Person();
22.     p.name = name[i];
23.     p.age = age[i];
25. }
26.

And second, don't reinvent sorting algrithms but use Collections.sort()
Expand|Select|Wrap|Line Numbers
1. // sort by name
2. class NameComparator implements Comparator {
3.     public int compare(Object p1, Object p2) {
4.         return ((Person)p1).name.compareTo(((Person)p2).name);
5.     }
6. }
7. Collections.sort(personList, new NameComparator());
8.
9. // print results
10. System.out.println("sorted person array=" + personList);
11.
So if you run these two code snippets, it will print out:
Expand|Select|Wrap|Line Numbers
1. sorted person array=[name: A-name age: 18, name: B-name age: 42]
2.
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, AddressComparator etc., and sort them by using
Expand|Select|Wrap|Line Numbers
1. Collections.sort(personList, new MarkComparator());
3.
You can even use anonymous inner classes for that, for example you do not declare a AgeComparator, but sort directly this way:
Expand|Select|Wrap|Line Numbers
1. Collections.sort(personList, new Comparator() {
2.     public int compare(Object p1, Object p2) {
3.         int age1 = ((Person)p1).age;
4.         int age2 = ((Person)p2).age;
5.         return (age1 > age2 ? 1 : (age1 == age2 ? 0 : -1));
6.     }
7. });
8.
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.