The task is to sort all the letters in a string alphabetically. The program should ignore case when sorting.

I wrote this code:

char[] charstr = str.toCharArray(); Arrays.sort(charstr, (char a , char b) -> {Character.compare(Character.toLowerCase(a) , Character.toLowerCase(b))}); str = new String(charstr); 

I receive a message that is not enough ; but the syntax seems to be correct. Who can tell what the error is?

  • Well, if the compiler says it is not enough; - it means all the same wrong syntax. after calling compare () (4 line) is necessary; - Nikita Gordeev
  • You forgot the semicolon right before the closing brace. It should be like this: .toLowerCase(b));}) . - ߊߚߤߘ

2 answers 2

Options:

  import java.util.Arrays; import java.util.Comparator; public class SortHelp { public static void main(String[] args) { String str = "bZDFghA"; String[] sAr = str.split(""); Arrays.sort(sAr, (o1, o2) -> o1.toLowerCase().compareTo(o2.toLowerCase())); System.out.println(Arrays.toString(sAr)); } } 

and

  import java.util.Arrays; import java.util.Comparator; public class SortHelp { public static void main(String[] args) { String str = "bZDFghA"; String[] sAr = str.split(""); Arrays.sort(sAr, Comparator.comparing(String::toLowerCase)); System.out.println(Arrays.toString(sAr)); } } 

    In the end, so decided:

     String[] split = str.split(""); Arrays.sort(split , String.CASE_INSENSITIVE_ORDER); str = new String(""); for (int i = 0 ; i < split.length; i++) str += split[i];