HTML & JS
// Java code to implement Radix sort 
// on array of strings

import java.io.*;
import java.util.Arrays;

public class GFG {
    
    // Function to perform Radix sort 
    // on String array    
    public static void radixSort(String input[], 
                                 int radix,
                                 int width)
    {
        for (int i = 0; i < width; i++) {
            performRadixSort(input, i, radix);
        }
    }
    public static void performRadixSort(String input[],
                                        int position,
                                        int radix)
    {
        
        // Creating a temporary count array
        int countArray[] = new int[radix];
        int nos = input.length;
        
        // Populating the count array
        for (String value : input) {
            countArray[getDigit(position, 
                                value, radix)]++;
        }
      
        // Normalizing count array
        for (int i = 1; i < radix; i++) {
            countArray[i]
                = countArray[i] 
                + countArray[i - 1];
        }
      
        String tempArray[] = new String[nos];
        // Building the final temporary array
        for (int i = nos - 1; i >= 0; i--) {
            tempArray[--countArray[getDigit(
                position, input[i], radix)]]
                = input[i];
        }
        // Copying into the actual array
        for (int i = 0; i < nos; i++) {
            input[i] = tempArray[i];
        }
    }
    // Hashing the input value, radix = 26
    // It takes the character at 
    // (length - position) location
    // and convert it to ascii value and 
    // return the ascii value
    public static int getDigit(int position, 
                               String value,
                               int radix)
    {
        int nos = value.length() - 1;
        char c = value.toLowerCase().charAt(nos - position);
        return (int)c - 97;
    }
     
    // Driver Code
    public static void main(String[] args)
    {
        String arr[]
            = { "BCDEF", "dbaqc", "abcde", "bbbbb" };
        System.out.println("Input:"
                           + Arrays.toString(arr));
        
        // Radix is the maximum value from 
        // the input array 
        // For String maximum value is 26
        radixSort(arr, 26, arr[0].length());
        System.out.println("Output:"
                           + Arrays.toString(arr));
    }
    
}

Generated URL:

Output:

Input:[BCDEF, dbaqc, abcde, bbbbb]
Output:[abcde, bbbbb, BCDEF, dbaqc]
Report Bug