A character array of arbitary length with 'R', 'B', 'W' is needed to be sorted in that order
Don't bother swapping.  Just do a counting sort!

public static void sort(char[] arr) {
    // Count occurrences of each letter
    int r = 0, b = 0, w = 0;
    for (char c : arr) {
        switch (c) {
          case 'R': r++; break;
          case 'B': b++; break;
          case 'W': w++; break;
          default: throw new IllegalArgumentException();
        }
    }

    // Write out the appropriate repetitions of each letter
    int i = 0;
    while (r-- &gt; 0) arr[i++] = 'R';
    while (b-- &gt; 0) arr[i++] = 'B';
    while (w-- &gt; 0) arr[i++] = 'W';
}


Not only is the code easy to understand, it's also gentle to the cache since you always proceed linearly down the array.