fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.time.LocalDate;
  7.  
  8. /* Name of the class has to be "Main" only if the class is public. */
  9. class Ideone
  10. {
  11. public static void main (String[] args) throws java.lang.Exception
  12. {
  13.  
  14. MyObject object0 = new MyObject(null, "2018-12-01");
  15. MyObject object1 = new MyObject(1, "2018-12-02");
  16. MyObject object2 = new MyObject(5, "2018-12-03");
  17. MyObject object3 = new MyObject(null, "2018-12-04");
  18. MyObject object4 = new MyObject(null, "2018-12-05");
  19. MyObject object5 = new MyObject(2, "2018-12-06");
  20. MyObject object6 = new MyObject(null, "2018-12-07");
  21.  
  22. List<MyObject> list = new ArrayList<>(Arrays.asList(object0, object1, object2, object3, object4, object5, object6));
  23. MyObject[] res = new MyObject[list.size()];
  24.  
  25. for (Iterator<MyObject> ite = list.iterator(); ite.hasNext(); ) {
  26. MyObject obj = ite.next();
  27. if (obj.getPriority() != null) {
  28. res[obj.getPriority() - 1] = obj;
  29. ite.remove();
  30. }
  31. }
  32.  
  33. list.sort(Comparator.comparing(MyObject::getCreatedAt).reversed());
  34.  
  35. System.out.println();
  36.  
  37. int indicList = 0;
  38. for (int i = 0; i < res.length; i++) {
  39. if (res[i] == null) {
  40. res[i] = list.get(indicList++);
  41. }
  42. }
  43.  
  44. for (MyObject re : res) {
  45. System.out.println(re);
  46. }
  47.  
  48. }
  49. }
  50.  
  51.  
  52. class MyObject {
  53.  
  54. private Integer priority;
  55.  
  56. private LocalDate createdAt;
  57.  
  58. MyObject(Integer priority, String createdAt) {
  59. this.priority = priority;
  60. this.createdAt = LocalDate.parse(createdAt);
  61. }
  62.  
  63. Integer getPriority() {
  64. return priority;
  65. }
  66.  
  67. LocalDate getCreatedAt() {
  68. return createdAt;
  69. }
  70.  
  71. @Override
  72. public String toString() {
  73. return "MyObject{" +
  74. "priority=" + priority +
  75. ", createdAt=" + createdAt +
  76. '}';
  77. }
  78. }
  79.  
Success #stdin #stdout 0.17s 2184192KB
stdin
Standard input is empty
stdout
MyObject{priority=1, createdAt=2018-12-02}
MyObject{priority=2, createdAt=2018-12-06}
MyObject{priority=null, createdAt=2018-12-07}
MyObject{priority=null, createdAt=2018-12-05}
MyObject{priority=5, createdAt=2018-12-03}
MyObject{priority=null, createdAt=2018-12-04}
MyObject{priority=null, createdAt=2018-12-01}