Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. list.sort(Comparator.comparing(report -> report.name))
  2.  
  3. list.sort(Comparator.comparing(report -> report.anyField))
  4.  
  5. String sortBy = "name";
  6. list.sort(Comparator.comparing(report -> {
  7. try {
  8. return (Comparable) report.getClass().getDeclaredField(sortBy).get(report);
  9. } catch (Exception e) {
  10. throw new RuntimeException("Ooops", e);
  11. }
  12. }));
  13.  
  14. String sortBy = "score";
  15. list.sort(Comparator.comparing(report -> Reflect.on(report).field(sortBy).get()));
  16.  
  17. static Map<String,Comparator<Report>> ORDER;
  18. static {
  19. HashMap<String,Comparator<Report>> m=new HashMap<>();
  20. m.put("name", Comparator.comparing(r -> r.name));
  21. m.put("date", Comparator.comparing(r -> r.date));
  22. m.put("score", Comparator.comparingInt(r -> r.score));
  23. ORDER=Collections.unmodifiableMap(m);
  24. }
  25. public static void sort(List<Report> list, String order) {
  26. Comparator<Report> c=ORDER.get(order);
  27. if(c==null) throw new IllegalArgumentException(order);
  28. list.sort(c);
  29. }
  30.  
  31. enum ReportOrder {
  32. NAME(Comparator.comparing(r -> r.name)),
  33. DATE(Comparator.comparing(r -> r.date)),
  34. SCORE(Comparator.comparingInt(r -> r.score));
  35.  
  36. private Comparator<Report> cmp;
  37. private ReportOrder(Comparator<Report> c) { cmp=c; }
  38.  
  39. public void sort(List<Report> list) {
  40. list.sort(cmp);
  41. }
  42. }
  43.  
  44. public static void sort(List<Report> list, ReportOrder o) {
  45. list.sort(o.cmp);
  46. }
  47.  
  48. sort(list, ReportOrder.DATE);
  49.  
  50. list.sort(Comparator.comparing(Report::getFieldName));
  51.  
  52. list.sort((ob1, ob2) -> ob1.getFieldName().compareTo(ob2.getFieldName()));
  53.  
  54. public static class MyBean {
  55. private String name;
  56. private Date birthday;
  57. private Integer score;
  58. ...
  59. ... (constructor, getters, setters - the usual bean stuff)
  60. }
  61.  
  62. PropertyDescriptor[] pdesc = Introspector.getBeanInfo(MyBean.class).getPropertyDescriptors();
  63. for(PropertyDescriptor pd : pdesc) {
  64. if(Comparable.class.isAssignableFrom(pd.getPropertyType())) {
  65. System.out.println("Property " + pd.getName() + " could be used for comparison");
  66.  
  67. Function<MyBean, Comparable> extractor = b -> {
  68. try {
  69. return (Comparable) pd.getReadMethod().invoke(b);
  70. }
  71. catch(Exception e) {
  72. throw new RuntimeException(e);
  73. }
  74. };
  75.  
  76. Comparator<MyBean> comp = Comparator.comparing(extractor);
  77.  
  78. // do something useful with the comparator :-)
  79. }
  80. }
  81.  
  82. private void someMethod(Comparator<Report> comparator){
  83. ...
  84. list.sort(comparator);
  85. ...
  86. }
  87.  
  88. ...
  89.  
  90. someMethod(Comparator.comparing(report::getName));
  91. someMethod(Comparator.comparing(report::getDate));
  92. someMethod(Comparator.comparingInt(report::getScore));
  93. someMethod(Comparator.comparing(report::getName).thenComparingInt(report::getScore));
  94.  
  95. public class Report {
  96.  
  97. //properties and getters
  98.  
  99. public static void sort(List<Report> list, Function<Report, Comparable> sortKey) {
  100. list.sort(Comparator.comparing(sortKey));
  101. }
  102. }
  103.  
  104. Report.sort(reports, Report::getName);
  105. Report.sort(reports, Report::getDate);
  106. Report.sort(reports, Report::getScore);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement