Results 1 to 14 of 14
  1. #1
    masaka is offline Member
    Join Date
    Mar 2008
    Posts
    31
    Rep Power
    0

    Question How to sort 2 arrays together

    Hi, i have 2 arrays the frist is to stroe students names and the second is for students grades i want to sort the names in alphabetical order and the grades moves to its names how to do this
    Java Code:
    import java.util.*;
    public class Main {
    static Scanner console= new Scanner(System.in);
        public static void main(String[] args) 
        {
       String []names=new String[10];    
       for(int i =0;i<names.length;i++)
       {
        System.out.print("Please Enter Student name ");
        System.out.println();
        names[i] =console.next();
        }   
        int []grade=new int[10];    
       for(int i =0;i<grade.length;i++)
       {
        System.out.print("Please Enter Student name ");
        System.out.println();
        grade[i] =console.nextInt();
        }
         
      }
    // Now I want to define a method that sort the names
    // and then moves the grade to its name    
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Use Arrays.sort() to sort an one array, keep the track if index and find the corresponding element of the next.

  3. #3
    masaka is offline Member
    Join Date
    Mar 2008
    Posts
    31
    Rep Power
    0

    Default

    please write code to explain more

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Best things is make an attempt to do something. :) Anyway here is a simple code, keep in mind this is not the only way to do this.

    Java Code:
    public class ArrayComparing {
    
        Scanner console= new Scanner(System.in);
        String[] names;
        String[] temp;
        int[] grade;
        
        public static void main(String[] args) {
            new ArrayComparing().getUserData();
        }
        
        private void getUserData() {
            names = new String[3];    
            for(int i = 0; i < names.length; i++) {
                System.out.print("Please Enter Student name: ");
                names[i] =console.nextLine();
                temp[i] = names[i];
            }
            
            grade = new int[3];    
            for(int i =0;i<grade.length;i++) {
                System.out.print("Please Enter Student marks: ");
                grade[i] =console.nextInt();
            }          
            
            sortArray(names);     
        }
        
        private void sortArray(String[] arrayToSort) {
            Arrays.sort(arrayToSort);
            
            getIndex(arrayToSort);
        }
        
        private void getIndex(String[] sortedArray) {
            for(int x = 0; x < sortedArray.length; x++) {
                for(int y = 0; y < names.length; y++) {
                    if(sortedArray[x].equals(temp[y])) {
                        System.out.println(sortedArray[x] + " " + grade[y]);
                    }
                }
            }
        }
    }

  5. #5
    masaka is offline Member
    Join Date
    Mar 2008
    Posts
    31
    Rep Power
    0

    Smile

    what is your opion of this code and is there another way to do this?
    this only the method used to sort the two arrayes
    Java Code:
    public void sort(String[] names, int[] grade){
        boolean swapped;
        do{
            swapped = false;
            for (int i = 0; i < names.length-1; i++) {
                if(names[i].compareToIgnoreCase(names[i+1])>0){
                    String temp = names[i+1];
                    names[i+1] = names[i];
                    names[i] = temp;
                    
                    int temp1 = grade[i+1];
                    grade[i+1] = grade[i];
                    grade[i] = temp1;
                        
                    swapped=true;
                }
            }
        }while(swapped);             
    }

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Ok, did you test that your code and get the correct answer? If so that's fine.

    Just look at my code. Used fewer number of lines to do this. At the time that is the way I get into my mind. :)

  7. #7
    masaka is offline Member
    Join Date
    Mar 2008
    Posts
    31
    Rep Power
    0

    Default

    it is work but how to divid this code into 2 methods the frist sort names and the second sort grades

  8. #8
    masaka is offline Member
    Join Date
    Mar 2008
    Posts
    31
    Rep Power
    0

    Smile now this the final code i want

    import java.util.*;
    public class Main {
    static Scanner console= new Scanner(System.in);
    public static void main(String[] args)
    {
    String []names=new String[2];
    for(int i =0;i<names.length;i++)
    {
    System.out.print("Please Enter Student name ");
    System.out.println();
    names[i] =console.next();
    }
    int []grade=new int[2];
    for(int i =0;i<grade.length;i++)
    {
    System.out.print("Please Enter Student grade ");
    System.out.println();
    grade[i] =console.nextInt();
    }
    sortname(names,grade);
    // here should be acall to amethod to prtint the arrays after sorting
    // i donnot know how to return the sorted arrays to make the print
    }
    public static void sortname(String[] names, int[] grade){
    boolean swapped;
    do{
    swapped = false;
    for (int i = 0; i < names.length-1; i++)
    {
    if(names[i].compareToIgnoreCase(names[i+1])>0)
    {
    String temp = names[i+1];
    names[i+1] = names[i];
    names[i] = temp;
    // here after sort the name we make acall to the second mehthod
    sortgrade(names,grade,i);
    swapped=true;
    }
    }
    }while(swapped);

    }
    public static void sortgrade(String []names,int[] grade ,int i)
    { // this method to sort the garde arry
    if(names[i].compareToIgnoreCase(names[i+1])>0)
    {
    int temp1 = grade[i+1];
    grade[i+1] = grade[i];
    grade[i] = temp1;
    }

    }
    // here will be the print method
    }

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  10. #10
    masaka is offline Member
    Join Date
    Mar 2008
    Posts
    31
    Rep Power
    0

    Default

    thanks it need some modify and finished at last
    thanks alot for you

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  12. #12
    masaka is offline Member
    Join Date
    Mar 2008
    Posts
    31
    Rep Power
    0

    Default

    import java.util.*;
    public class Main {
    static Scanner console = new Scanner(System.in);
    public static void main(String[] args)
    {
    String []names=new String[2];
    for(int i =0;i<names.length;i++)
    {
    System.out.print("Please Enter Student name ");
    System.out.println();
    names[i] =console.next();
    }
    int []grade=new int[2];
    for(int i =0;i<grade.length;i++)
    {
    System.out.print("Please Enter Student grade ");
    System.out.println();
    grade[i] = console.nextInt();
    }
    sortname(names,grade);
    print (names,grade);
    }
    public static void sortname(String[] names, int[] grade){
    boolean swapped;
    do{
    swapped = false;
    for (int i = 0; i < names.length-1; i++)
    {
    if(names[i].compareToIgnoreCase(names[i+1])>0)
    {
    String temp = names[i+1];
    names[i+1] = names[i];
    names[i] = temp;
    sortgrade(names, grade,i);
    swapped=true;
    }
    }
    }while(swapped);
    }
    public static void sortgrade(String []names,int[] grade ,int i)
    { int temp1 = grade[i+1];
    grade[i+1] = grade[i];
    grade[i] = temp1;
    }
    public static void print(String []names,int[] grade )
    {
    for (int i = 0; i < names.length; i++)
    System.out.print(names[i]+" "+grade[i]+"\n");
    }
    }

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Yep, your attempt is not bad. How you going to handle more than two entries at a time. I mean using this code only can sorted two students.

  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Always I try to pointed out, try to use minimum number of variables. In your sortgrade() method you have not refer string array names. If you are not use it, better to avoid declaring such things.

Similar Threads

  1. How to sort a list using Bubble sort algorithm
    By Java Tip in forum Algorithms
    Replies: 3
    Last Post: 04-29-2008, 09:04 PM
  2. Arrays
    By bunbun in forum New To Java
    Replies: 1
    Last Post: 04-09-2008, 03:24 AM
  3. new to arrays
    By jimJohnson in forum New To Java
    Replies: 1
    Last Post: 04-08-2008, 03:45 PM
  4. 2D-Arrays
    By kbyrne in forum New To Java
    Replies: 1
    Last Post: 02-07-2008, 11:08 PM
  5. arrays help
    By Warren in forum New To Java
    Replies: 6
    Last Post: 11-23-2007, 08:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •