Java tutorial
//package com.java2s; //License from project: Apache License public class Main { private static int safeCompare(Object f1, Object f2) { if (f1 == null && f2 == null) return 0; if (f1 == null && f2 != null) return -1; if (f1 != null && f2 == null) return 1; if (f1.equals(f2)) return 0; return safeCompare(f1.toString(), f2.toString()); } public static int safeCompare(String f1, String f2) { if (f1 == null && f2 == null) return 0; if (f1 == null && f2 != null) return -1; if (f1 != null && f2 == null) return 1; return f1.compareTo(f2); } }