Results 1 to 1 of 1
- 10-04-2011, 10:45 PM #1
Member
- Join Date
- Jun 2011
- Posts
- 46
- Rep Power
- 0
How to use Comparator/Comparable to sort Hash Map of Objects by value within object
I am trying to figure out how to use Comparator / Comparable to sort a Hash Map <Integer, Object> by value within the object. I have been searching for examples but haven't quite found what I am looking for. I'll post a generic situation below.
Program asks user if they would like to sort by id number, Name or School.
public class someClass{
//code to enter data from file into ArrayList of Person objects
//code to assign ArrayList to Hash Map of Person objects using int idNumber as key. <int idNumber, Person object>
}
public class Person{
int idNumber;
String Name;
String School;
//constructor to initialize variables
//getIdNumber(), getName, getSchool methods defined
}
public class SortByName implements Comparator<Person>{
public int compare(Person t, Person t1) {
//some code to compare
}
}
I am unsure about how to implement this, especially if the user is given multiple choices in how they would like to sort the map. If somebody can offer an example or some direction I would greatly appreciate it. This is the only part of my program that has given me problems.
Similar Threads
-
Comparable and Comparator
By jeanjiang in forum New To JavaReplies: 7Last Post: 04-23-2011, 07:59 AM -
how to sort in this hash map
By akira_mz86@yahoo.com in forum New To JavaReplies: 6Last Post: 03-17-2010, 02:19 AM -
Convert Comparable object to string or char
By ScKaSx in forum New To JavaReplies: 4Last Post: 01-25-2009, 02:02 PM -
Creating a Comparable object
By Java Tip in forum java.langReplies: 0Last Post: 04-15-2008, 07:38 PM -
Using Comparable and Comparator interfaces
By barney in forum New To JavaReplies: 1Last Post: 08-07-2007, 07:10 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks