fork(1) download
  1. import java.util.Arrays;
  2. import java.util.Comparator;
  3.  
  4. public class Main {
  5. public static void main( String[] args ) {
  6. String[] dictionary = new String[] {"SAD", "PAD", "SAP", "BAD", "SIT"};
  7. System.out.println( "Before sort dictionary: " + Arrays.toString(dictionary) );
  8. Arrays.sort(dictionary, dialPadNumCompare);
  9. System.out.println( "After sort dictionary: " + Arrays.toString(dictionary) );
  10. }
  11. public static Comparator<String> dialPadNumCompare = new Comparator<String>(){
  12. @Override
  13. public int compare(String a, String b){
  14. int inta = stringToInt(a);
  15. int intb = stringToInt(b);
  16. if (inta == intb)
  17. return a.compareTo(b);
  18. return inta - intb;
  19. }
  20. };
  21. public static int stringToInt(String msg){
  22. int out = 0;
  23. for (int i = 0; i < msg.length(); i++ ){
  24. out *= 10;
  25. out += charToDigit(msg.charAt(i));
  26.  
  27. }
  28. return out;
  29. }
  30. public static int charToDigit(char a){
  31. if (a > ('A' - 1) && a <= 'C'){
  32. return 2;
  33. }
  34. if (a > 'C' && a <= 'F'){
  35. return 3;
  36. }
  37. if (a > 'F' && a <= 'I'){
  38. return 4;
  39. }
  40. if (a > 'I' && a <= 'L'){
  41. return 5;
  42. }
  43. if (a > 'L' && a <= 'O'){
  44. return 6;
  45. }
  46. if (a > 'O' && a <= 'S'){
  47. return 7;
  48. }
  49. if (a > 'S' && a <= 'V'){
  50. return 8;
  51. }
  52. if (a > 'V' && a <= 'Z'){
  53. return 9;
  54. }
  55. return 0;
  56. }
  57. }
Success #stdin #stdout 0.02s 245632KB
stdin
Standard input is empty
stdout
Before sort dictionary: [SAD, PAD, SAP, BAD, SIT]
After sort dictionary: [BAD, PAD, SAD, SAP, SIT]