In FullComparator , sorting by several fields ( channelName , DateCreated (in reverse order) and Fingerprint ) does not work. Sorts only by the first. What am I doing wrong?
public class FullComparator implements Comparator<Capability> { @Override public int compare(Capability o1, Capability o2) { if (o1 != null && o2 != null) { if (o1.getChannelName() != null && o2.getChannelName() != null && !o1.getChannelName().equals(o2.getChannelName())) return o1.getChannelName().compareTo(o2.getChannelName()); if (o1.getFingerprint() != null && o2.getFingerprint() != null && !o1.getFingerprint().equals(o2.getFingerprint())) return o1.getFingerprint().compareTo(o2.getFingerprint()); if (o1.getDateCreated() != null && o2.getDateCreated() != null && !o2.getDateCreated().equals(o1.getDateCreated())) return o2.getDateCreated().compareTo(o1.getDateCreated()); } if (o1 == null && o2 != null) return 1; if (o1 != null) return -1; return 0; } } public class Capability implements Comparable<Capability> { private long id; private String channelName; private String fingerprint; private boolean isActive; private Date dateCreated; public Capability(long id, String channelName, String fingerprint, boolean isActive, Date dateCreated) { this.id = id; this.channelName = channelName; this.fingerprint = fingerprint; this.isActive = isActive; this.dateCreated = dateCreated; } public long getId() { return id; } public String getChannelName() { return channelName; } public String getFingerprint() { return fingerprint; } public boolean isActive() { return isActive; } public Date getDateCreated() { return dateCreated; } @Override public String toString() { return "Capability{" + "id=" + id + ", channelName='" + channelName + '\'' + ", fingerprint='" + fingerprint + '\'' + ", isActive=" + isActive + ", dateCreated=" + dateCreated + '}'; } public class DemoComparator { public static void main(String[] args) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd"); Capability capability1 = new Capability(1001, "A", "R", true, sdf.parse("2015-12-15")); Capability capability2 = new Capability(1005, "D", "Z", false, sdf.parse("2010-12-15")); Capability capability3 = new Capability(900, null, "O", true, sdf.parse("2013-12-15")); Capability capability4 = new Capability(900, "B", "L", false, sdf.parse("2008-12-15")); ArrayList<Capability> capabilities = new ArrayList<>(); capabilities.add(capability1); capabilities.add(capability2); capabilities.add(null); capabilities.add(capability4); System.out.println(capabilities); System.out.println(); capabilities.sort(new FullComparator()); System.out.println(capabilities); } } And unless the method can sort all fields? Then it turns out that the objects need to change fields so that at the same time, in the final result, all the fields are in the correct order.
Capabilityclass to the question code. 2. Why did you decide that sorting does not work? If thechannelNamematches, the objects are quite self sorted byfingerprint. Similar todateCreatedwhen the values of the first two fields match. - Regent