Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

write a method that takes an array of strings and orders the strings in the arra

ID: 3678898 • Letter: W

Question

write a method that takes an array of strings and orders the strings in the array by the number of vowels (a, e, i, o, or u) in each string (from fewest to most vowels)

If two strings have the same number of vowels, order them by which one comes first alphabetically, ignoring case. For instance, when called like this from main:

main: String[] strings = {"hello", "apple", "hat", "cat", "Banana", "AAA"};

System.out.println(Arrays.toString(strings));

orderByVowels(strings);

System.out.println(Arrays.toString(strings));

The output should be:

[hello, Apple, hat, cat, Banana, AAA]

[cat, hat, Apple, hello, AAA, Banana]

Explanation / Answer

package sophy;

import java.util.Arrays;
/*
* This class is to sort the given String of array from fewest count vowels to highest number of vowels count
*/
public class SortWords {
   /*
   * Main method to invoke our logical method.
   */
   public static void main(String os[]){
       //array of strings.
       String[] strings = {"hello", "apple", "hat", "cat", "Banana", "AAA"};
       //Displaying the initial array..
       System.out.println(Arrays.toString(strings));
       orderByVowels(strings);
       //calling the method to sort array..

   }

  
   public static void orderByVowels(String[] naaaa)
{
for(int i=0;i<naaaa.length;i++){
for(int j=i+1;j<naaaa.length;j++)
{
if(naaaa[i].compareTo(naaaa[j])>0)
{
String tppp = naaaa[i];
naaaa[i] = naaaa[j];
naaaa[j]=tppp;
}
}
  
}
for(int i=0; i<naaaa.length;i++){
     
   System.out.print(naaaa[i]+" ");
  
}
  
}
     
  

}