简体   繁体   中英

How can I sort a string array in ascending order, by number of vowels in each string?

    import java.util.*;
    class VowelAsc
    {
        public static void main(String args[])
        {
            int count=0;
            Scanner sc=new Scanner(System.in);
            int n=sc.nextInt();
            String [] s=new String[n];
            int [] b=new int[40];
            for(int i=0;i<n;i++)
            { 
                s[i]=sc.next();
            }
            for(int i=0;i<s.length;i++)
            {
                char[] a=s[i].toCharArray();
                for(int c=0;c<a.length;c++)
                {

                    if(a[c]=='a' || a[c]=='e' || a[c]=='i' || a[c]=='o' || a[c]=='u' ||a[c]=='A' ||a[c]=='E' || a[c]=='I' || a[c]=='O' || a[c]=='U')
                    {
                       count++;
                       //b[c]=count;
                    }
                }
                if(count>0)
                {
                    if(i<s.length)
                    {
                        String t=s[i];
                        s[i]=s[i+1];
                        s[i+1]=t;
                    }
                } 
           }
      }
 }

I am trying to count the vowels present in each string and i wanted to swap the strings based on count variable which i am unable to do. After accepting the strings i am converting it into char array with toCharArray() function and comparing each character with lower and upper case vowels.

I am getting an error. Any help in writing the part of the code would be appreciated.

Input:

n=4
xyz
bad
aeiou
hello

Output:

aeiou
hello
bad
xyz

Well... this might be slightly over the top (List and RegEx), but if you don't have to execute this millions of times, then sorting them in a list via a custom Comparator will do the job:

String[] s = new String[]{"xyz", "bad", "aeiou", "hello"};

Arrays.sort(s, new Comparator<String>(){
    @Override
    public int compare(String o1, String o2) {
        return o2.replaceAll("[^aeiouAEIOU]", "").length()
                - o1.replaceAll("[^aeiouAEIOU]", "").length();
    }
});

EDIT: Optimized by removing the List, thanks @Holger

Similar to mumpitz code, but optimized for performance:

class Comp implements Comparator<String> {
  private static boolean[] isVowel = new boolean[127];
  static {
    isVowel['a'] = true;
    isVowel['e'] = true;
    isVowel['i'] = true;
    isVowel['o'] = true;
    isVowel['u'] = true;
  }
  @Override

  public int compare(String o1, String o2) {
    return count(o1) - count(o2); 
  }

  private int count(String s) {
    int cnt = 0;
    for (int i=0; i<s.length(); i++) {
      char c = s.charAt(i);
      if (c < 128 && isVowel(c))
        cnt++;
      }
    }
    return cnt;
  }
}

String[] s = new String[]{"xyz", "bad", "aeiou", "hello"};
List<String> sList = Arrays.asList(s);

Collections.sort(sList, new Comp());
s = sList.toArray(s);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM