Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Sorting parallel arrays

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Sorting parallel arrays

    I want a method to sort the parallel arrays according to the ascending order of names.
    The code is working however the names I enter is not sorted by ascending order. What am I doing wrong?

    public class SecA {
     
        //Method to store names,ages and marks for 10 people into parallel arrays
        public static void recAray(String[] name,int [] age,double[] mark)
        {
            for(int i=0;i<name.length;i++)//For loop to prompt to enter 10 names,ages and marks based on the names length array
            {
                Scanner in=new Scanner(System.in);//Creating Scanner object "in" to get input form the keyboard in the recAray method 
                  System.out.println("Enter name");//Prompting to enter name
                    name[i]=in.nextLine();
                        System.out.println("Enter age");//Prompting to enter age
                            age[i]=in.nextInt();
                                System.out.println("Enter mark");//Prompting to enter age
                                    mark[i]=in.nextDouble();
     
                 //Displaying names, ages and marks in array element [i]              
                System.out.println("Person is:"+" "+name[i]+" "+"Age is:"+" "+age[i]+" "+" "+"Mark is:"+
                                    " "+mark[i]);
            }
        }
     
     
     
        //Method to sort the parallel arrays according to the ascending order of names.        
        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;
     
                                      /*
                                       *   Swap marks using the same index
                                       *   of the age
                                       */
                                           double markTemp=mark[j];
                                                    mark[j]= mark[j + 1];
                                                        mark[j + 1]=markTemp;
                    }
     
               }
          }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Sorting parallel arrays

    Please edit the post and fix the formatting of the statements. Statements at the same level of logic should start in the same column and NOT continually be indented.


    You ask:
    the names I enter is not sorted by ascending order
    The code says:
    *Swap age with lowest age

    * Swap name using the same index
    * of the age
    The code's comments imply the sorting is based on the age, NOT on the name.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Sorting parallel arrays

    Yes,,thats what I'm doing. How do i swap the names. I don't know how to compare them as they are Strings.

  4. #4
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Sorting parallel arrays

    If comparing strings is all you need to figure out. Look at the javadocs for the compareTo() method. This should handle everything you need.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

Similar Threads

  1. Arrays sorting
    By LeeDD in forum What's Wrong With My Code?
    Replies: 13
    Last Post: March 4th, 2013, 03:17 PM
  2. Two Parallel Arrays
    By jsmnr77 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 31st, 2013, 09:41 AM
  3. Counting Matches in Parallel Arrays
    By dx8292 in forum Object Oriented Programming
    Replies: 3
    Last Post: February 1st, 2012, 09:45 AM
  4. Sorting Arrays
    By Bryan29 in forum Collections and Generics
    Replies: 5
    Last Post: November 28th, 2011, 07:21 AM
  5. Help with code dealing with parallel arrays.
    By danielp1213 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 13th, 2011, 07:43 PM