Skip to content

Commit

Permalink
create new list in sort method to avoid side effects
Browse files Browse the repository at this point in the history
  • Loading branch information
BartChris authored and solth committed Oct 30, 2023
1 parent c2213a6 commit 8d51e53
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -983,15 +983,16 @@ private Process loadParentProcess(Ruleset ruleset, int projectId, String parentI
* @param projectId projectId by which the list gets sorted
*/
public List<ProcessDTO> sortProcessesByProjectID(List<ProcessDTO> processDTOs, int projectId) {
List<ProcessDTO> sortedList = new ArrayList<>(processDTOs);
Comparator<ProcessDTO> comparator = Comparator.comparingInt(obj -> {
if (obj.getProject().getId() == projectId) {
return 0; // Matching value should come first
} else {
return 1; // Non-matching value comes later
}
});
Collections.sort(processDTOs, comparator);
return processDTOs;
Collections.sort(sortedList, comparator);
return sortedList;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ public void shouldSortProcessesWithProvidedProjectIdFirst() {
List<ProcessDTO> processes = new ArrayList<>(Arrays.asList(processOne, processTwo, processThree));

ImportService importService = new ImportService();
importService.sortProcessesByProjectID(processes, 9);
List<ProcessDTO> sortedProcesses = importService.sortProcessesByProjectID(processes, 9);

int projectIdOfFirstProcess = processes.get(0).getProject().getId();
int projectIdOfFirstProcess = sortedProcesses.get(0).getProject().getId();

Assert.assertEquals("Process not sorted based on provided projectId",9,
projectIdOfFirstProcess);
Expand Down

0 comments on commit 8d51e53

Please sign in to comment.