Skip to content
Advertisement

Tag: sorting

Sorting multiple conditions with null value

I have this code, which sorts an array list based on 3 variables But the last variable, getValidFrom can be null since it is OffsetDateTime. If it is null, it throws a NullPointerException. Is there a way to handle null values sorting this way? Or I have to create my own comparing method? Answer Use the nullsFirst() (null < non-null)

Sort List of Strings arrays in Java

I’m trying to make a program that consists of an array of Strings . now I need to sort them in order ‘PJ’, ‘GHS’, ‘FJ’, ‘PAS’ and date. input : output: //Always ‘PJ’, ‘GHS’, ‘FJ’ then ‘PAS’ Answer You should consider using custom java classes instead of String Array. Find below an example to do it .

Sorting an array of classes based only on field name

I have an application where a user provides me with the name of a field, e.g name or costInCents, and I have to sort by that field. I have ways of guaranteeing that the field name will be correct. This application causes the complication that I simply cannot make my class Comparable and implement a specific compareTo(), since with a

Advertisement