Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Conversation Between aussiemcgr and alisa

3 Visitor Messages

  1. I would then create a method to compare with each type (by gender, by year, and by last name). In order to compare objects more effectively, I would send these comparing objects Person objects:
    	public int compareGender(Person p)
    		String tempGen = p.gender;
    		return gender.compareTo(tempGen);
    	public int compareYear(Person p)
    		String tempDate = p.date;
    		String[] tDateSplit = tempDate.split("/");
    		int tempYear = Integer.parseInt(tDateSplit[2]);
    		String[] dateSplit = date.split("/");
    		int year = Integer.parseInt(dateSplit[2]);
    		return year-tempYear;
    	public int compareLastName(Person p)
    		String tempLN = p.lastName;
    		return lastName.compareTo(tempLN);

    See if you can figure out how to implement this to compare the objects and resort the arraylist.

    I hope this helps. It is quite simple to figure this sort of thing out. You just have to work through the problem one step at a time and try to tackle each issue individually.
  2. Well, if there were no limitations, I would create an object (Person Object perhaps). This object would take a First Name, a Last Name, a Gender, a Date, and an Eye Color:
    public class Person
    	String firstName,lastName,gender,date,eyeColor;	
    	public Person(String fName,String lName,String gen,String dat,String eColor)
    		firstName = fName;
    		lastName = lName;
    		gender = gen;
    		date = dat;
    		eyeColor = eColor;
  3. hi All,

    could you please help me with this :

    my ArrayList contains objects with person information :

    Abercrombie Neil Male 2/13/1943 Tan,
    Bishop Timothy Male 4/23/1967 Yellow,
    Bonk Radek Male 6/3/1975 Green,
    Bouillon Francis Male 6/3/1975 Blue,

    I need to get it sorted 3 different ways and print on console 3 sorted sets :

    1. by gender
    2. by year desc
    3. by last name desc

    do I need to create Comparators, or ..?

    Thank you !
Showing Visitor Messages 1 to 3 of 3