0
votes

Good evening.

I need to organize an HashMap, and it is something like this:

private HashMap < String , Vector < Task > > tasksOrdered = new HashMap < String , Vector < Task > > ();

Each "Task" is a made class with 3 attributes:

  • Name of Person;
  • Hour;
  • Discipline;

I know that with that HashMap i can group Tasks by hours into vectors, just by setting the tasks hours as key, and adding those vectors, or instances if there are existing vectors.

My question is, how can i order those vectors by name of person?

An example of what i want:

[1]: Table https://dl.dropbox.com/u/4134140/Untitled.png

Hope it helps :S

3

3 Answers

3
votes

You can order by person name like this:

SortedMap<String, List<Task>> taskMap = new ConcurrentSkipListMap<String, List<Task>>();
taskMap.put(task.getPerson().getName(), task);
0
votes

HashMap doesn't give you any ordering. However, I am not sure if you are really interested in sorting?

It seems like you want to group tasks based off of name? You can do the same by inserting the persons name into the hashmap to grab the vector. If you want to retrieve them ordered by name, you can grab the keyset(), call java.util.Collections.sort(), and then retrieve the vectors in order.

0
votes

Key = task and value = name of person

Collections.sort(Vector, new Comparator() {

        @Override
         public int compare(Vector o1, Vector o2) {
             return Collator.getInstance().compare(o1.getvalue(), o2.getValue());
         }
    });

Just psuedo, a bit of tweaking might be needed.