I don't believe Java has an equivalent to the array_count_values function, so you will need to implement that yourself. Something like this:
public static <T> Map<T, Integer> countValues(List<T> values) {
Map<T, Integer> result = new HashMap<T, Integer>();
// iterate through values, and increment its corresponding value in result
return result;
}
Then use the java.util.Collections.sort(List list, Comparator c) function to sort the array by the counts. You'll need to implement Comparator to sort by the counts.
public class CountComparator<T> implements Comparator<T> {
private Map<T, Integer> counts;
public CountComparator(Map<T, Integer> counts) {
this.counts = counts;
}
public int compare(T o1, T o2) {
// assumes that the map contains all keys
return counts.get(o1).compareTo(counts.get(o2));
}
}
arsortdoesn't sort by the value in the array. It sorts by the key.