Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implements Comparable to get alphabetical sort with Strings

I would like an object to be comparable (to use it in a TreeSet in that case).

My object got a name field and I would like it to be sorted by alphabetical order.

I thought first that I could use the unicode value of the string and simply do a subtraction, but then AA would be after Ab for example…

Here’s how I started :

public final class MyObject implements Comparable<MyObject> {

 private String name;

 public MyObject(String name) {
  this.name = name;
 }

 public String name() {
  return name;
 }

 @Override
 public int compareTo(MyObject otherObject) {
  return WHAT DO I PUT HERE ?;
 }
}

Thanks to those who will help, have a nice day!

like image 446
Silver Duck Avatar asked Apr 04 '14 19:04

Silver Duck


People also ask

How do you sort strings using comparable?

Programmers frequently need to sort elements from a database into a collection, array, or map. In Java, we can implement whatever sorting algorithm we want with any type. Using the Comparable interface and compareTo() method, we can sort using alphabetical order, String length, reverse alphabetical order, or numbers.

How do you compare strings in alphabetical order?

If you are truly comparing Strings alphabetically to arrange them in order, use compareTo() method from Comparable interface in Java. It also compare String based upon there value, and can be used to sort String alphabetically, if they are stored in List using Collections.

Which interface should be implemented for sorting comparable?

So now we know that if we want to sort java object array or list, we need to implement java Comparable interface to provide default sorting and we should implement java Comparator interface to provide different ways of sorting.

How do you sort a string alphabetically in Python?

Summary. Use the Python List sort() method to sort a list in place. The sort() method sorts the string elements in alphabetical order and sorts the numeric elements from smallest to largest. Use the sort(reverse=True) to reverse the default sort order.


2 Answers

You are overthinking the problem. Strings have their own natural ordering, which is alphabetic, so you can just use the String.compareTo like this:

@Override
public int compareTo(MyObject otherObject) {
    return this.name.compareTo(otherObject.name);
}
like image 142
azurefrog Avatar answered Oct 01 '22 22:10

azurefrog


Exist so many way which preferred before it. But for maintain better compatibility, performance and avoiding runtime exceptions (such as NullPointerException) use best practices which is

For String

@Override
    public int compareTo(OtherObject o) {
        return String.CASE_INSENSITIVE_ORDER.compare(this.name,o.name);
    }

For int, double float (to avoid boxing and unboxing which issue for performance use below comparators)

// with functional expression
Comparator.compareInt, Comparator.compareDouble, Comparator.compareFloat


// or with static compare method
/**
*  Integer
*/
public int compareTo(Integer anotherInteger) {
        return compare(this.value, anotherInteger.value);
    }

/**
*  Double
*/
public int compareTo(Double anotherDouble) {
        return Double.compare(value, anotherDouble.value);
    }

/**
*  Float
*/
public int compareTo(Float anotherFloat) {
        return Float.compare(value, anotherFloat.value);
    }

/**
*  Objects
*/
public int compareTo(Object other) {
        return Object.compare(value, other.value);
    }

[Effective Java Item 14: Consider implement Comparable]

Finally, whenever you implement a value class that has a sensible ordering, you should have a class implements Comparable interface so that its instances can be easily sorted, searched and used in comparison-based collections. When comparing field values in the implementations of the compareTo methods, avoid the use of the < and > operators. Instead, use the static compare methods in the boxed primitive classes or the comparator construction methods in the Comparator interface

like image 45
Musa Avatar answered Oct 01 '22 21:10

Musa