1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
| private ArrayList<DBTask> sort(ArrayList<DBTask> taskArrayList) { ArrayList<DBTask> sortedDBTaskList = new ArrayList<>(); if (taskArrayList == null || taskArrayList.size() <= 0) { return sortedDBTaskList; } ArrayList<DBTask> label_0_DBTaskList = new ArrayList<>(); ArrayList<DBTask> label_1_DBTaskList = new ArrayList<>(); ArrayList<DBTask> label_2_DBTaskList = new ArrayList<>(); ArrayList<DBTask> label_3_DBTaskList = new ArrayList<>(); ArrayList<DBTask> finished_DBTaskList = new ArrayList<>();
for (DBTask dbTask : taskArrayList) { if (dbTask.getIsFinish()) { finished_DBTaskList.add(dbTask); continue; } switch (dbTask.getLabel()) { case DBTask.LABEL_IMPORTANT_URGENT: label_0_DBTaskList.add(dbTask); break; case DBTask.LABEL_IMPORTANT_NOT_URGENT: label_1_DBTaskList.add(dbTask); break; case DBTask.LABEL_NOT_IMPORTANT_URGENT: label_2_DBTaskList.add(dbTask); break; case DBTask.LABEL_NOT_IMPORTANT_NOT_URGENT: label_3_DBTaskList.add(dbTask); break; } } mergeSort2List(label_0_DBTaskList, sortedDBTaskList); mergeSort2List(label_1_DBTaskList, sortedDBTaskList); mergeSort2List(label_2_DBTaskList, sortedDBTaskList); mergeSort2List(label_3_DBTaskList, sortedDBTaskList); mergeSort2List(finished_DBTaskList, sortedDBTaskList);
return sortedDBTaskList; }
private void reverse(ArrayList<DBTask> arr, int i, int j) { while(i < j) { DBTask temp = arr.get(i); arr.set(i++, arr.get(j)); arr.set(j--, temp); } }
private void swapAdjacentBlocks(ArrayList<DBTask> arr, int bias, int oneSize, int anotherSize) { reverse(arr, bias, bias + oneSize - 1); reverse(arr, bias + oneSize, bias + oneSize + anotherSize - 1); reverse(arr, bias, bias + oneSize + anotherSize - 1); }
private void inplaceMerge(ArrayList<DBTask> arr, int l, int mid, int r) { int i = l; int j = mid + 1; while(i < j && j <= r) { while(i < j && isValid(arr, i, j)) { i++; } int index = j; while(j <= r && isValid(arr, j, i)) { j++; } swapAdjacentBlocks(arr, i, index-i, j-index); i += (j-index); } }
private boolean isValid(ArrayList<DBTask> arr, int i, int j) { Date date_i = TimeUtil.formatGMTDateStr(arr.get(i).getCreated_datetime()); Date date_j = TimeUtil.formatGMTDateStr(arr.get(j).getCreated_datetime()); return (date_i != null ? date_i.getTime() : 0) <= (date_j != null ? date_j.getTime() : 0); }
private void mergeSort(ArrayList<DBTask> arr, int l, int r) { if(l < r) { int mid = (l + r) / 2; mergeSort(arr, l, mid); mergeSort(arr, mid + 1, r); inplaceMerge(arr, l, mid, r); } }
private void mergeSort2List(ArrayList<DBTask> taskArrayList, ArrayList<DBTask> result) { if (taskArrayList == null || taskArrayList.size() <= 0) { return; } mergeSort(taskArrayList, 0, taskArrayList.size()-1); result.addAll(taskArrayList); }
|