11 Replies - 3883 Views - Last Post: 30 April 2012 - 10:39 AM Rate Topic: -----

#1 huphane  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 01-April 12

Sorting two string arrays alphabetically

Posted 03 April 2012 - 02:04 PM

public class Sorting {

    /**   A program sorting two  string arrays alphabetically


     * @param args the command line arguments
     */


     
    public static void main(String[] args) {
        
        
         String A[] = { "Joseph","Ariana","Xena","Rene","Peter","Diana",
     "Rihanna"};


        String B[] = {"Madona","Diddy","Joe","Alice"};



         for(int j = 0; j < A.length; j++) {    \\ My problem begins here

       for(int i = 0 ; i < B.length; i++) { 

          if(B[i].compareToIgnoreCase(A[j]) < 0) { 

           String K = A[j]; 


           A[j] = B[i]; 


            B[i] = K; 
              } 
        } 
              System.out.println(A[j]);      // This prints wrongly

        }
    
 
      } 
         } 



I can not get the whole list sorted? Can anyone have an idea. may be first merge and then

sorting using compareTo method.

This post has been edited by jon.kiparsky: 03 April 2012 - 02:05 PM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Sorting two string arrays alphabetically

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Sorting two string arrays alphabetically

Posted 03 April 2012 - 02:23 PM

That's a very odd problem....
How are use supposed to re-arrange them ? The first 10 names (of both arrays) in alphabetical order in the first array and the last 4 ones in the second array ?

If you want to merge them,

Sort first the two list within themselve
Create an array the size of the two arrays and merge the lists into that array

or create an array the size of both arrays. Copy both arrays into that newly created one.
Then sort with the algorithm you want that single array
Was This Post Helpful? 0
  • +
  • -

#3 huphane  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 01-April 12

Re: Sorting two string arrays alphabetically

Posted 04 April 2012 - 08:19 AM

View Posthuphane, on 03 April 2012 - 02:04 PM, said:

public class Sorting {

    /**   A program sorting two  string arrays alphabetically


     * @param args the command line arguments
     */


     
    public static void main(String[] args) {
        
        
         String A[] = { "Joseph","Ariana","Xena","Rene","Peter","Diana",
     "Rihanna"};


        String B[] = {"Madona","Diddy","Joe","Alice"};



         for(int j = 0; j < A.length; j++) {    \\ My problem begins here

       for(int i = 0 ; i < B.length; i++) { 

          if(B[i].compareToIgnoreCase(A[j]) < 0) { 

           String K = A[j]; 


           A[j] = B[i]; 


            B[i] = K; 
              } 
        } 
              System.out.println(A[j]);      // This prints wrongly

        }
    
 
      } 
         } 



I can not get the whole list sorted? Can anyone have an idea. may be first merge and then

sorting using compareTo method.


I have tried to merge them in Array C[] beu i can not sort the new array.
Was This Post Helpful? 0
  • +
  • -

#4 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • Posts: 3,002
  • Joined: 30-January 11

Re: Sorting two string arrays alphabetically

Posted 04 April 2012 - 08:22 AM

import java.util.Arrays;
...
...

Arrays.sort(mergedArray);
?

This post has been edited by Ryano121: 04 April 2012 - 08:22 AM

Was This Post Helpful? 0
  • +
  • -

#5 huphane  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 01-April 12

Re: Sorting two string arrays alphabetically

Posted 04 April 2012 - 01:06 PM

I need to use compareTo method and sort the new array c?
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Sorting two string arrays alphabetically

Posted 04 April 2012 - 01:48 PM

View Posthuphane, on 04 April 2012 - 11:19 AM, said:

I have tried to merge them in Array C[] beu i can not sort the new array.

Where is that ?
Was This Post Helpful? 0
  • +
  • -

#7 huphane  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 01-April 12

Re: Sorting two string arrays alphabetically

Posted 06 April 2012 - 07:47 AM

View Postpbl, on 04 April 2012 - 01:48 PM, said:

View Posthuphane, on 04 April 2012 - 11:19 AM, said:

I have tried to merge them in Array C[] beu i can not sort the new array.

Where is that ?


          public static void main(String[] args) {
        
        
         String A[] = { "Joseph","Ariana","Xena","Rene","Peter","Diana",
"Rihanna"};
        String B[] = {"Madona","Diddy","Joe","Alice"};
        
        
        for(int j = 0; j < A.length; j++) {
            for(int s = j +1; s < B.length; s++)
for(int i = 0 ; i < B.length; i++) { 
if(A[j].compareToIgnoreCase(B[i]) < 0) { 

} 
} 
    
       List<String> list = new ArrayList(Arrays.asList(A));
      list.addAll(Arrays.asList(B)/>);
      Object[] C = list.toArray();
      
      
      System.out.println(Arrays.toString(C));

        
        

    }
}
        }



*Edited: added code tags
:code:

This post has been edited by pbl: 06 April 2012 - 07:08 PM

Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Sorting two string arrays alphabetically

Posted 06 April 2012 - 07:09 PM

Re-read by post #2

Create an array the size of both arrays
Merge the two arrays into that new one
Sort the merged array
Was This Post Helpful? 0
  • +
  • -

#9 huphane  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 01-April 12

Re: Sorting two string arrays alphabetically

Posted 08 April 2012 - 03:12 AM

View Postpbl, on 06 April 2012 - 07:09 PM, said:

Re-read by post #2

Create an array the size of both arrays
Merge the two arrays into that new one
Sort the merged array



I have merged them already,but i can not sort the new array. i did it like this;
                  import java.util.ArrayList;
                  import java.util.Arrays;
                  import java.util.List;
 
           String A[] = { "Joseph","Ariana","Xena","Rene","Peter","Diana",
           "Rihanna"};
           String B[] = {"Madona","Diddy","Joe","Alice"};
        
        List<String> list = new ArrayList(Arrays.asList(A));
        list.addAll(Arrays.asList(B)/>);
      
         Object [] C = list.toArray();
      
         System.out.println(Arrays.toString(C));


but you can access the merged Arrays C?

*Edited added code tags. :code:

This post has been edited by pbl: 08 April 2012 - 01:32 PM

Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Sorting two string arrays alphabetically

Posted 08 April 2012 - 01:43 PM

I though you have to add your own sorting algorithm.
Using the one of the API it is a piece of cake

	public static void main(String[] arg) {
		String Ar[] = { "Joseph","Ariana","Xena","Rene","Peter","Diana", "Rihanna"};
		String Br[] = {"Madona","Diddy","Joe","Alice"};
		ArrayList<String> list = new ArrayList<String>(Arrays.asList(Ar));
		list.addAll(Arrays.asList(Br));

		String [] C = list.toArray(new String[0]);
		Arrays.sort(C);

		System.out.println(Arrays.toString(C));
	}


Was This Post Helpful? 0
  • +
  • -

#11 huphane  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 01-April 12

Re: Sorting two string arrays alphabetically

Posted 30 April 2012 - 10:30 AM

View Postpbl, on 08 April 2012 - 01:43 PM, said:

I though you have to add your own sorting algorithm.
Using the one of the API it is a piece of cake

	public static void main(String[] arg) {
		String Ar[] = { "Joseph","Ariana","Xena","Rene","Peter","Diana", "Rihanna"};
		String Br[] = {"Madona","Diddy","Joe","Alice"};
		ArrayList<String> list = new ArrayList<String>(Arrays.asList(Ar));
		list.addAll(Arrays.asList(Br));

		String [] C = list.toArray(new String[0]);
		Arrays.sort(C);

		System.out.println(Arrays.toString(C));
	}


Was This Post Helpful? 0
  • +
  • -

#12 huphane  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 01-April 12

Re: Sorting two string arrays alphabetically

Posted 30 April 2012 - 10:39 AM

Thanks D.I.C,

I did it in another way. But how about if i want to sort it in loop control. Is that possible?

E.G you have two already sorted arrays like those ones:

String [] A = {"Alice","Diana","Xena"};
String [] B = {"Madona","Jackson"};
Can you compare them one by one until you get arranged alphabetically?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1