Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,037 questions

40,846 answers

573 users

How to sort a string in Java

2 Answers

0 votes
import java.util.Arrays;

public class MyClass {
    public static String sort(String s) {
        char[] arr = s.toCharArray();

        Arrays.sort(arr);
          
        return new String(arr);
    }
    public static void main(String args[]) {
        String s = "java programming";
    
        s = sort(s);
        
        System.out.println(s);
    }
}


/*
run:

 aaaggijmmnoprrv

*/

 





answered Oct 26, 2019 by avibootz
0 votes
import java.util.stream.Collectors;
import java.util.stream.Stream;
 
public class MyClass {
    public static String sort(String str) {
        str = Stream.of(str.split(""))
                    .sorted()
                    .collect(Collectors.joining());
           
        return str;
    }
    public static void main(String args[]) {
        String s = "java programming";
     
        s = sort(s);
         
        System.out.println(s);
    }
}
 
 
/*
run:
 
 aaaggijmmnoprrv
 
*/

 





answered Apr 29 by avibootz

Related questions

1 answer 73 views
73 views asked Oct 31, 2020 by avibootz
1 answer 101 views
2 answers 134 views
134 views asked Dec 13, 2016 by avibootz
1 answer 88 views
1 answer 72 views
...