Sorting

Java Sort char Array Example

Want to learn quickly?
Try one of the many quizzes. More than Java 400 questions with detailed answers.

2 Comments

  • public static void stringSort(String string){

    /* String to character array */
    char[] charArray = string.toCharArray();

    /* Iterate through the array */
    for (int i = 0; i < string.length – 1; i++){
    for (int j = i + 1; j < string.length; j++){

    int st = Character.toLowerCase(charArray[i])
    – Character.toLowerCase(charArray[j]);
    /* Case insensitive comparison */
    if ( st == 0 )
    /* letters are the same */
    st = charArray[i] – charArray[j];

    /* Case sensitive comparison */
    if (st > 0){
    int temp = charArray[i];
    charArray[i] = charArray[j];
    charArray[j] = temp;
    }
    }
    }
    }

Sponsors

Facebook Fans