skip to Main Content
public int userCompare(String s1, int i1, String s2, int i2){
        String[] alphabety = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
        String[] firstThing = s1.split("(?!^)");
        String[] secondThing = s2.split("(?!^)");
}

How do I complete the following:

Create a method called userCompare that will return a -1, 1, or 0 based on the following: We have data for two users, A and B, each with a String name and an int id. The goal is to order the users such as for sorting. Return -1 if A comes before B, 1 if A comes after B, and 0 if they are the same. Order first by the string names, and then by the id numbers if the names are the same.

2

Answers


  1. I’d compare each component individually, and then use signum to get strictly 1, -1 or 0.

    public int userCompare(User a, User b) {
        int retVal = a.getName().compareTo(b.getName);
        if (retVal != 0) {
            return (int) Math.signum(retVal);
        }
    
        retVal = Integer.compare(a.getId(), b.getId());
        return (int) Math.signum(retVal);
    }
    

    Side note – for the purpose of sorting in Java, you don’t need to strictly return 1, -1 or 0, you can just return an arbitrary positive, negative or zero value. In other words, the usage of signum is required by question, but isn’t strictly required for sorting.

    Login or Signup to reply.
  2. I would utilize the String#compareTo, and Integer#compare methods here.

    int userCompare(String nameA, int idA, String nameB, int idB) {
        int t;
        if ((t = nameA.compareTo(nameB)) == 0)
            return Integer.compare(idA, idB);
        return t;
    }
    

    If you’re looking to implement the actual comparison routine, review the source for each of those methods.
    For a String, it’s just a traversal of the bytes.

    Here is utilized StringLatin1#compareTo method.
    GitHub – OpenJDK – StringLatin1.java – compareTo(byte[], byte[], int, int).

    public static int compareTo(byte[] value, byte[] other, int len1, int len2) {
        int lim = Math.min(len1, len2);
        for (int k = 0; k < lim; k++) {
            if (value[k] != other[k]) {
                return getChar(value, k) - getChar(other, k);
            }
        }
        return len1 - len2;
    }
    

    And, for an Integer it’s just a relational, and equality, operation.
    GitHub – OpenJDK – Integer.java – compare(int, int).

    /**
     * Compares two {@code int} values numerically.
     * The value returned is identical to what would be returned by:
     * <pre>
     *    Integer.valueOf(x).compareTo(Integer.valueOf(y))
     * </pre>
     *
     * @param  x the first {@code int} to compare
     * @param  y the second {@code int} to compare
     * @return the value {@code 0} if {@code x == y};
     *         a value less than {@code 0} if {@code x < y}; and
     *         a value greater than {@code 0} if {@code x > y}
     * @since 1.7
     */
    public static int compare(int x, int y) {
        return (x < y) ? -1 : ((x == y) ? 0 : 1);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search