1

I am trying to sort a char[] array in Java.

I tried using a List but it didn't work perfectly.
I also tried using Arrays.sort(dizimiz,String.case_insensive_order), but I got an error.

Here is the code I have:

Scanner sc=new Scanner(System.in);
System.out.println("Please enter text:");
String str=sc.nextLine();

//  toCharArray() splits the string into a character array
char[] chars=str.toCharArray();

Arrays.sort(chars);
for (char c : chars) {
    System.out.println(c);
}

What method can I use to do this?

4
  • 1
    docs.oracle.com/javase/7/docs/api/java/util/… Commented Apr 16, 2014 at 21:08
  • use String.equalsIgnoreCase() Commented Apr 16, 2014 at 21:09
  • 3
    "it didn't work perfectly" and "i got error" are not problem descriptions. (In any language.) How did it not work? What error did you get? Commented Apr 16, 2014 at 21:09
  • Why would you want to sort the characters in a character array? Why not put them in a UnicodeSet? Commented Apr 16, 2014 at 21:12

2 Answers 2

2

The comparator String.CASE_INSENSITIVE_ORDER is for strings only, but you tried using it with a char array. If you want to use it, you have to convert the string into an array of strings (each string containing a single character).

String[] chars = str.split("");
Arrays.sort(chars, String.CASE_INSENSITIVE_ORDER);

for (String c : chars) {
    System.out.println(c);
}
Sign up to request clarification or add additional context in comments.

1 Comment

This certainly is a lot easier than my answer. But I wonder which answer is more efficient at 1 million characters?
0

If you do not care about the case you can just change

char[] dizimiz = str.toCharArray();

to

char[] dizimiz = str.toLowerCase().toCharArray();

That will accomplish your question

How can i sort char[] array case insensitive?

If there is something else I am missing here please let me know and I will update my answer.

As per the comments below I have made a rough code for you to sort and retain the output with the proper casing.

    char[] dizimiz = str.toCharArray();
    char[] lc = str.toLowerCase().toCharArray();
    char temp;
    Boolean done;
    if (lc.length > 1) do {
        done = true;
        for (int i = lc.length - 1; i > 0; i--) {
        if (lc[i] < lc[i - 1]) {
            temp = lc[i - 1];
            lc[i - 1] = lc[i];
            lc[i] = temp;
            temp = dizimiz[i - 1];
            dizimiz[i - 1] = dizimiz[i];
            dizimiz[i] = temp;
            done = false;
           }
        }
    } while (!done);

8 Comments

No, that won't. If the OP has b, A, a, C, he wants A, a, b, C as a result, not a, a, b, c
Yes its true.I want A,a,b,C.How can i sort like this.
@JBNizet I updated code to do that. I know there is easier ways but I like to see how it is being done in the for loop.
@user3542379 I updated my answer it will output the way you want it.
@CodeCamper: You're trying to reimplement a sort algorithm, and your algorithm isn't correct. You'd better transform the char array to a Character array, sort it using the standard algorithm and a custom Comparator<Character>, and then transform the Character array back to a char array.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.