Issue: I am trying to check if I can sort the list (ArrayList of data type Object), using 2 or more criteria's. Please note I am aware of using Comparator with thenComparing feature. I wish to check if there is a way to sort with 2 or more criteria w/o having to use a custom data type where I can easily use Comparator feature. For 1 criteria sorting, the below code works.
In this case if I do the below, the IntelliJ IDE immediately gives a error saying - 'Cannot resolve method 'get(int)' for o.get(3)
.sorted(Comparator.comparing(o -> o.get(3).toString()).thenComparing(...)
I have also referred to many threads in this forum sample - Link1 Link2
Code (This works well for single criteria)
List<List<Object>> listOutput = new ArrayList<>();
.........
.........
listOutput = listOutput
.stream()
.sorted(Comparator.comparing(o -> o.get(3).toString()))
.collect(Collectors.toList());
Added (Details of Object)
(Note)
String dataType - exchange, broker,segment, coCode
LocalDate dataType - tradeDate
LocalTime dataType - tradeTime
double dataType - sellPrice, buyPrice
List<Object> lineOutput = new ArrayList<>();
lineOutput.add(exchange);
lineOutput.add(broker);
lineOutput.add(segment);
lineOutput.add(tradeDate); // Entry Date
lineOutput.add(tradeTime); // Entry Time
lineOutput.add(coCode);
lineOutput.add(sellPrice - buyPrice); // Profit / Loss
listOutput.add(lineOutput); // Add line to Output