By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,806 Members | 1,363 Online
Bytes IT Community
+ Ask a Question
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
  1.  
  2. public void sortD(String[]name,int[] age, double[] mark) 
  3.     {
  4.         for (int i = 0; i < age.length - 1; i++) 
  5.         {
  6.             for (int j = 0; j < age.length -1-i; j++) 
  7.             {
  8.                 if (age[j] > age[j + 1]) 
  9.                 {
  10.         /*
  11.         *Swap age with lowest age
  12.         */
  13.                 int ageTemp = age[j];
  14.                         age[j] = age[j + 1];
  15.                               age[j + 1] = ageTemp;
  16.  
  17.              /*
  18.                Swap name using the same index of the age
  19.                        */
  20.  
  21.                         String nameTemp = name[j];
  22.                            name[j] = name[j + 1];
  23.                              name[j + 1] = nameTemp;
  24.  
  25.                 double markTemp=mark[j];
  26.                   mark[j]= mark[j + 1];
  27.                    mark[j + 1]=markTemp;                                               
  28.                 }
  29.  
  30.            }
  31.  
Mar 26 '13 #1

✓ answered by chaarmann

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];    
  24.     personList.add(p);
  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());
  2. Collections.sort(personList, new AddressComparator());
  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.

Share this Question
Share on Google+
2 Replies


Rabbit
Expert Mod 10K+
P: 12,422
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?
Mar 26 '13 #2

Expert 100+
P: 785
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];    
  24.     personList.add(p);
  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());
  2. Collections.sort(personList, new AddressComparator());
  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.
Mar 28 '13 #3

Post your reply

Sign in to post your reply or Sign up for a free account.