Guest User

Untitled

a guest
Jul 17th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.Collections;
  4. import java.util.Comparator;
  5.  
  6. public class FB {
  7.  
  8. public static int comparator(String s1, String s2) {
  9.  
  10. String[] pt1 = s1.split("((?<=[a-z])(?=[0-9]))|((?<=[0-9])(?=[a-z]))");
  11. String[] pt2 = s2.split("((?<=[a-z])(?=[0-9]))|((?<=[0-9])(?=[a-z]))");
  12.  
  13. int i=0;
  14. if(Arrays.equals(pt1, pt2))
  15. return 0;
  16. else{
  17. for(i=0;i<Math.min(pt1.length, pt2.length);i++)
  18. if(!pt1[i].equals(pt2[i])) {
  19. if(!isNumber(pt1[i],pt2[i])) {
  20. if(pt1[i].compareTo(pt2[i])>0)
  21. return 1;
  22. else
  23. return -1;
  24. }
  25. else {
  26. int nu1 = Integer.parseInt(pt1[i]);
  27. int nu2 = Integer.parseInt(pt2[i]);
  28. if(nu1>nu2)
  29. return 1;
  30. else
  31. return -1;
  32. }
  33. }
  34. }
  35.  
  36. if(pt1.length>i)
  37. return 1;
  38. else
  39. return -1;
  40. }
  41.  
  42. private static Boolean isNumber(String n1, String n2) {
  43. // TODO Auto-generated method stub
  44. try {
  45. int nu1 = Integer.parseInt(n1);
  46. int nu2 = Integer.parseInt(n2);
  47. return true;
  48. }
  49. catch(Exception x) {
  50. return false;
  51. }
  52. }
  53.  
  54. public static void main(String[] args) {
  55. // TODO Auto-generated method stub
  56.  
  57. ArrayList<String> values = new ArrayList<String>();
  58. values.add("aa9");
  59. values.add("a");
  60. values.add("9");
  61. values.add("a9a");
  62. values.add("9aa");
  63. values.add("abc");
  64. values.add("999");
  65. values.add("9a9");
  66.  
  67. System.out.println(values);
  68. Comparator<String> com = (o1,o2) -> {return comparator(o1,o2);}; //lambda expression
  69. Collections.sort(values,com);
  70. System.out.println(values);
  71.  
  72. }
  73. }
Add Comment
Please, Sign In to add comment