MindBrain
MindBrain

Reputation: 7768

TreeSet not sorting correctly

I have a self written comparator in java for comparing two name strings.

public class NamesSorter implements Comparator<EntityBean>{
    @Override
    public int compare(EntityBean a1, EntityBean a2) {
       return a1.getName().compareToIgnoreCase(a2.getName());
    }
}

The main code using this sorter

final Set<EntityBean> treeSet = new TreeSet<EntityBean>(new NamesSorter());
final List<EntityBean> firstListToCompare=display.getAvailablePeople().getItems(); 
final List<EntityBean> SecondListToCompare=display.getAvailablePeople().getItems(); 
treeSet.addAll(firstListToCompare);
treeSet.addAll(secondListToCompare);       

When I run this code with the following input

firstListToCompare amelia,george,megan

secondListToCompare George Wash,Fang.

The result is

amelia, Fang, george, megan, George Wash.

If I have an ignore case in the comparator why is it pushing George Wash to the end ?

the getName method.

public String getName() { return name; }

Any name with a space between the two strings Like George Wash (which has a space between George and Wash) goes to the end of the list.. Why is that so ?

Upvotes: 1

Views: 3390

Answers (2)

Thomas W
Thomas W

Reputation: 14164

Any funny characters / whitespace in the data? The poster's code has @Override and that doesn't give any errors. Unlike equals()/ hashCode(), there would be no Object method to default to -- so that's not what's happening.

I think this may be a data error.

Put some logging, or a breakpoint, in the Comparator method & see what's happening there.

public class NamesSorter implements Comparator<EntityBean>{
    @Override
    public int compare (EntityBean a1, EntityBean a2) {
       String name1 = a1.getName();
       String name2 = a2.getName();
       // let's try trimming them..
       name1 = name1.trim();
       name2 = name2.trim();
       int comp = name1. compareToIgnoreCase( name2);
       log.debug("  name CI comparison: "+name1+", "+name2+"="+comp);
       // System.out.println("  name CI comparison: "+name1+", "+name2+"="+comp);
       return comp;
    }
}

Logging is your friend.

Upvotes: 0

neo108
neo108

Reputation: 5246

I am assuming your getName() is returning a String? Why not try adding toString() to getName(). So try...

public int compare(EntityBean a1, EntityBean a2) {
   return a1.getName().toString().compareToIgnoreCase(a2.getName().toString());
}

I just tried your code as below and it is giving me the correct output...

public class NamesSorter implements Comparator<Object>{
    public int compare(Object arg0, Object arg1) {
        return (arg0.toString()).compareToIgnoreCase(arg1.toString());
    }
}

public static void main(String[] args) {
    Comp comp = new Comp();
    final Set<Object> treeSet = new TreeSet<Object>(comp.new NamesSorter());
    final List<Object> firstListToCompare = new ArrayList<Object>(); 
    firstListToCompare.add("amelia");
    firstListToCompare.add("george");
    firstListToCompare.add("megan");
    final List<Object> secondListToCompare = new ArrayList<Object>();
    secondListToCompare.add("George Wash");
    secondListToCompare.add("Fang");

    treeSet.addAll(firstListToCompare);
    treeSet.addAll(secondListToCompare);
    for (Iterator<Object> iter = treeSet.iterator(); iter.hasNext(); ) {
        System.out.println((String) iter.next());
    }
}

The result...

amelia
Fang
george
George Wash
megan

Upvotes: 2

Related Questions