import java.util.Arrays;
import java.util.Comparator;

public class Main {
    public static void main( String[] args ) {
		String[] dictionary = new String[] {"SAD", "PAD", "SAP", "BAD", "SIT"};
		System.out.println( "Before sort dictionary: " + Arrays.toString(dictionary) );
	    Arrays.sort(dictionary, dialPadNumCompare);
	    System.out.println( "After sort dictionary: " + Arrays.toString(dictionary) );
	}
	public static Comparator<String> dialPadNumCompare = new Comparator<String>(){
	    @Override
	    public int compare(String a, String b){
	        int inta = stringToInt(a);
	        int intb = stringToInt(b);
	    	if (inta == intb)
	            return a.compareTo(b);
	        return inta - intb;
	    }
	};
	public static int stringToInt(String msg){
	    int out = 0;
	    for (int i = 0; i < msg.length(); i++ ){
	        out *= 10;
	        out += charToDigit(msg.charAt(i));

	    }
	    return out;
	}
	public static int charToDigit(char a){
	    if (a > ('A' - 1) && a <= 'C'){
	        return 2;
	    }
	    if (a > 'C' && a <= 'F'){
	        return 3;
	    }
	    if (a > 'F' && a <= 'I'){
	        return 4;
	    }
	    if (a > 'I' && a <= 'L'){
	        return 5;
	    }
	    if (a > 'L' && a <= 'O'){
	        return 6;
	    }
	    if (a > 'O' && a <= 'S'){
	        return 7;
	    }
	    if (a > 'S' && a <= 'V'){
	        return 8;
	    }
	    if (a > 'V' && a <= 'Z'){
	        return 9;
	    }
	    return 0;
	}
}