public int compare(Object o1, Object o2) {

      X509Certificate cert1 = (X509Certificate) o1;
      X509Certificate cert2 = (X509Certificate) o2;

      /*
       * if either cert certifies the target, always
       * put at head of list.
       */
      if (cert1.getSubjectX500Principal().equals(targetSubjectDN)) {
        return -1;
      }
      if (cert2.getSubjectX500Principal().equals(targetSubjectDN)) {
        return 1;
      }

      int targetDist1;
      int targetDist2;
      try {
        X500Name targetSubjectName = X500Name.asX500Name(targetSubjectDN);
        targetDist1 = Builder.targetDistance(null, cert1, targetSubjectName);
        targetDist2 = Builder.targetDistance(null, cert2, targetSubjectName);
      } catch (IOException e) {
        if (debug != null) {
          debug.println("IOException in call to Builder.targetDistance");
          e.printStackTrace();
        }
        throw new ClassCastException("Invalid target subject distinguished name");
      }

      if (targetDist1 == targetDist2) return 0;

      if (targetDist1 == -1) return 1;

      if (targetDist1 < targetDist2) return -1;

      return 1;
    }