Derek Ball :
I have a list of objects that contain values stored as strings but are actually numbers and I'm trying to find a way to parse strings to integers within a Java stream to sort them numerically. Here's what I got so far.
List<SearchResultObject> returnedValues = doTheSearchMethod();
returnedValues = returnedValues
.stream()
.sorted(Comparator.comparing(SearchResultObject::getNumber))
.collect(Collectors.toList());
As of right now this kind of works, but the problem is that because those numbers are actually strings, it sorts the numbers lexicographically, not numerically. Is there a way to incorporate parsing into the stream? All numbers are padded to be the same size.
3356001017053000000000000
3356001017002000000000000
3356001017004000000000000
3356001017026000000000000
5101004003020000000000000
4123000006002510000000000
4758000005010000000000000
mavriksc :
use comparingLong
and then parse long. the numbers are too large for integers.
returnedValues.sort(Comparator.comparingLong(sro->Long.parseLong(sro.getNumber())));