Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort duplicated Strings using comparator?

Say i have a list containing workers, each workers has 3 fields: its name, the department he's working in (can be just the name of the department or an Object from the class Department) and his salary.

Elvis       Software Engineering        1000
Samba       Mechanical Engineering      2000
Bamba       Industrial Engineering      3000
Bisli       Medical Engineering         4000
Kinder      Electrical Engineering      1000
Elvis       Software Engineering        9999

now i want to sort them by their names and put the result in a Queue. and than put the queue in a map, ordered from the bottom to the top so the result i desire after the sort is:

Bamba       Industrial Engineering      3000
Bisli       Medical Engineering         4000
Elvis       Software Engineering        1000
Elvis       Software Engineering        9999
Samba       Mechanical Engineering      2000
Kinder      Electrical Engineering      1000

I'm not allowed to use Collection.sort(), so i'm using a comparator that sorts the workers by their names, if the name is equal - it sorts by the department, if the department is equal - it sorts by the salary. here's the comparator i wrote:

    class WorkerComparatorByName implements Comparator<Worker<?>> {
    @Override
    public int compare(Worker<?> w1, Worker<?> w2) {
        int compareValue = w1.getName().compareTo(w2.getName());
        if (compareValue != 0)
            return compareValue;
        compareValue = w1.getDepartment().toString().compareTo(w2.getDepartment().toString());
        if (compareValue != 0)
            return compareValue;
        return w1.getSalary() - w2.getSalary();

    }
}

problem is that the result is this:

Bamba       Industrial Engineering      3000
Bisli       Medical Engineering         4000
Elvis       Software Engineering        1000
Samba       Mechanical Engineering      2000
Kinder      Electrical Engineering      1000
Elvis       Software Engineering        9999

all the workers are sorted, but Elvis (which is duplicated) is NOT sorted, it stays at the end of the queue. I tried replacing Elvis with another duplicated name, and same result. What am i missing? how can i sort duplicated value so they'll be one after the other? Here's the code:

    public <T extends Worker<?>> Map<?, ?> createMap(ArrayList<T> list) {
    int i = 1;
    // creating a PriorityQueue sorted by names
    Queue<T> pq = new PriorityQueue<>(new WorkerComparatorByName());
    // filling the PriorityQueue with the workers
    pq.addAll(list);
    Map<Integer, T> treeMap = new TreeMap<Integer, T>();
    // iterating over the PriorityQueue and puting the workers in the map
    for (T element : pq)
        treeMap.put(i++, element);
    return treeMap;
}
like image 679
Robert Avatar asked Dec 14 '17 12:12

Robert


1 Answers

PriorityQueue API:

The Iterator provided in method iterator() is not guaranteed to traverse the elements of the priority queue in any particular order. If you need ordered traversal, consider using Arrays.sort(pq.toArray()).

like image 133
Evgeniy Dorofeev Avatar answered Sep 30 '22 09:09

Evgeniy Dorofeev