Skip to content

Commit

Permalink
update. sort logic for numeric bed numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
Arjun-Go committed Jun 18, 2024
1 parent 16c0fa0 commit 7bb54b0
Showing 1 changed file with 20 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;

@Service
Expand Down Expand Up @@ -40,11 +40,12 @@ public IPDPatientDetails getIPDPatientByWard(String wardUuid, Integer offset, In
if (admittedPatients ==null ){
return new IPDPatientDetails(new ArrayList<>(),0);
}
List<AdmittedPatient> admittedPatientsSortedList = Objects.equals(sortBy, "bedNumber") ? sortNumericBedNumbers(admittedPatients) : admittedPatients;

offset = Math.min(offset, admittedPatients.size());
limit = Math.min(limit, admittedPatients.size() - offset);
offset = Math.min(offset, admittedPatientsSortedList.size());
limit = Math.min(limit, admittedPatientsSortedList.size() - offset);

return new IPDPatientDetails(admittedPatients.subList(offset, offset + limit), admittedPatients.size());
return new IPDPatientDetails(admittedPatientsSortedList.subList(offset, offset + limit), admittedPatientsSortedList.size());
}

@Override
Expand All @@ -55,12 +56,12 @@ public IPDPatientDetails getIPDPatientsByWardAndProvider(String wardUuid, String
if (admittedPatients ==null ){
return new IPDPatientDetails(new ArrayList<>(),0);
}
List<AdmittedPatient> admittedPatientsSortedList = sortBedNumbers(admittedPatients);
List<AdmittedPatient> admittedPatientsSortedList = Objects.equals(sortBy, "bedNumber") ? sortNumericBedNumbers(admittedPatients) : admittedPatients;

offset = Math.min(offset, admittedPatientsSortedList.size());
limit = Math.min(limit, admittedPatientsSortedList.size() - offset);

return new IPDPatientDetails(admittedPatientsSortedList.subList(offset, offset + limit), admittedPatients.size());
return new IPDPatientDetails(admittedPatientsSortedList.subList(offset, offset + limit), admittedPatientsSortedList.size());
}

@Override
Expand All @@ -73,32 +74,28 @@ public IPDPatientDetails searchIPDPatientsInWard(String wardUuid, List<String> s
return new IPDPatientDetails(new ArrayList<>(),0);
}

List<AdmittedPatient> admittedPatientsSortedList = sortBedNumbers(admittedPatients);
List<AdmittedPatient> admittedPatientsSortedList = Objects.equals(sortBy, "bedNumber") ? sortNumericBedNumbers(admittedPatients) : admittedPatients;

offset = Math.min(offset, admittedPatientsSortedList.size());
limit = Math.min(limit, admittedPatientsSortedList.size() - offset);

return new IPDPatientDetails(admittedPatientsSortedList.subList(offset, offset + limit), admittedPatients.size());
return new IPDPatientDetails(admittedPatientsSortedList.subList(offset, offset + limit), admittedPatientsSortedList.size());
}

private List<AdmittedPatient> sortBedNumbers(List<AdmittedPatient> admittedPatients) {
Collections.sort(admittedPatients, (patientA, patientB) -> {
String bedNumberA = patientA.getBedPatientAssignment().getBed().getBedNumber();
String bedNumberB = patientB.getBedPatientAssignment().getBed().getBedNumber();
private List<AdmittedPatient> sortNumericBedNumbers(List<AdmittedPatient> admittedPatients) {

boolean isNumericA = numericPattern.matcher(bedNumberA).matches();
boolean isNumericB = numericPattern.matcher(bedNumberB).matches();
boolean allNumeric = admittedPatients.stream()
.map(patient -> patient.getBedPatientAssignment().getBed().getBedNumber())
.allMatch(bedNumber -> numericPattern.matcher(bedNumber).matches());

if (isNumericA && isNumericB) {
return Integer.compare(Integer.parseInt(bedNumberA), Integer.parseInt(bedNumberB));
} else if (!isNumericA && !isNumericB) {
return bedNumberA.compareTo(bedNumberB);
} else {
return isNumericA ? -1 : 1;
}
});
if(allNumeric) {
admittedPatients.sort((patientA, patientB) -> {
String bedNumberA = patientA.getBedPatientAssignment().getBed().getBedNumber();
String bedNumberB = patientB.getBedPatientAssignment().getBed().getBedNumber();

return Integer.compare(Integer.parseInt(bedNumberA), Integer.parseInt(bedNumberB));
});
}
return admittedPatients;

}
}

0 comments on commit 7bb54b0

Please sign in to comment.