Question

I have a hashmap with values that both contain and do not contain asterisks (* ). I want to sort my values so that all values with an (* ) come last. I tried using this line of code:

    SortedSet<String> orderedValues = new TreeSet<String>(myHashMap.values());

but it orders it alphabetically then withing that places all values with an (*) first. is there anyway to reverse that???

Was it helpful?

Solution 3

you have to write your own java.util.Comparator to do customized sorting. Default String sorting is alphabetical.

EDITED to meet new requirements to group sorting by no asterisks, then all with 1 then all with 2 etc

If you want to group sorting by no asterisks, then all with 1 then all with 2 etc and then inside each grouping, sort alphabetically, then you probably want something like this:

public class AsterisksLast implements Comparator<String>{

@Override
public int compare(String o1, String o2) {


    int numAsterisksInO1 = numberOfAsterisksIn(o1);
    int numAsterisksInO2 = numberOfAsterisksIn(o2);

    if(numAsterisksInO1 == numAsterisksInO2){
        //both contain same number of asterisks
        //(which may be 0)
        //sort 
        //alphabetically
        return o1.compareTo(o2);
    }
    //different number of asterisks sort by fewest
    return Integer.compare(numAsterisksInO1, numAsterisksInO2);

}

private int numberOfAsterisksIn(String s){
    char[] chars =s.toCharArray();
    int count=0;
    for(int i=0; i<chars.length; i++){
        if(chars[i] == '*'){
            count++;
        }
    }
    return count;
}

}

then you need to give it to your TreeSet

SortedSet<String> orderedValues = new TreeSet<String>(new AsterisksLast());
orderedValues.addAll(myHashMap.values());

OTHER TIPS

You should just define a custom Comparator for Strings and pass that in as the Comparator when you build the TreeSet. Here's a similar StackOverflow question with a detailed answer:

TreeSet Custom Comparator Algo .. String Comparision

You haven't specified exactly what you're doing with the asterisk, but below is an example that sorts based on if the string contains an asterisk. Note that I use a List<String> here instead of a HashMap. The fact that you're getting the values from a HashMap isn't actually important—you'd do the same thing for any Java Collection.

List<String> strs = Arrays.asList("a","b","c","a*","b*");
Comparator<String> starCompare = new Comparator<String>() {
  @Override public int compare(String a, String b) {
    int aStar = a.contains("*") ? 1 : 0;
    int bStar = b.contains("*") ? 1 : 0;
    return (aStar != bStar)
      ? Integer.compare(aStar, bStar)
      : a.compareTo(b);
  }
};
// Without comparator
SortedSet<String> set1 = new TreeSet<String>(strs);
System.out.println(set1); // => [a, a*, b, b*, c]
// With comparator
SortedSet<String> set2 = new TreeSet<String>(starCompare);
set2.addAll(strs);
System.out.println(set2); // => [a, b, c, a*, b*]

Notice how for the output of set2, which uses the custom Comparator, the entries including an * are last in the sorted set.

You can reverse your Treeset

 TreeSet<String> treeSetObj = new TreeSet<String>( Collections.reverseOrder() ) ;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top