Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3275648
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:10:04+00:00 2026-05-17T19:10:04+00:00

I have a task to play with Java Collections framework. I need to obtain

  • 0

I have a task to play with Java Collections framework. I need to obtain a users list from a database, and store it in a collection. (This is finished and users are stored in a HashSet). Each user is an instance of Person class described with name, surname, birth date, join date, and some other parameters which are not important now. Next I need to store the list in different collections (there’s nowhere stated how many) providing functionality to sort them by:
– name only
– name, surname, birthdate
– join date

Ok, so to start with, my Person stores data as Strings only (should I change dates to Date ?). I’ve started implementing sorting with “by name, surname, birthdate”, cause that’s what I get after calling sort on list with Strings. Am I right ?

public List createListByName(Set set){
    List ret = new ArrayList<String>();
    String data = "";

    for(Object p: set){
        data = p + "\n";
        ret.add(data);
    }
    Collections.sort(ret);
    return ret;
}

But what with the rest ? Here’s my Person :

class Person {

    private String firstname;
    private String lastname;
    )..)


    Person(String name, String surname, (..)){
        firstname = name;
        lastname = surname;
        (..)
    }

    @Override
    public String toString(){
        return firstname + " " + lastname + " " + (..);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T19:10:04+00:00Added an answer on May 17, 2026 at 7:10 pm

    I wouldn’t convert everything to strings to start with. I would implement Comparator<Person> and then sort a List<Person>:

    public List<Person> createListByName(Set<Person> set){
        List<Person> ret = new ArrayList<Person>(set);
        Collections.sort(ret, new NameSurnameBirthComparator());
        return ret;
    }
    

    The NameSurnameBirthComparator would implement Comparator<Person> and compare two people by first comparing their first names, then their surnames (if their first names are equal) then their birth dates (if their surnames are equal).

    Something like this:

    public int compare(Person p1, Person p2) {
        // TODO: Consider null checks, and what to do :)
        int firstNameResult = p1.getFirstName().compareTo(p2.getFirstName());
        if (firstNameResult != 0) {
            return firstNameResult;
        }
        int surnameResult = p1.getSurname().compareTo(p2.getSurname());
        if (surnameResult != 0) {
            return surnameResult;
        }
        return p1.getBirthDate().compareTo(p2.getBirthDate());
    }
    

    And yes, I would store the date of birth as a Date – or preferably as a LocalDate from JodaTime, as that’s a much nicer library for date and time manipulation 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using dhtmlx Gantt Chart UI component which have task list and graphical chart.
I am new this sharepoint development and i have task in hand to do
I have some task regarding do operations on database using ADODB in Vb.NET. Can
I have a task to do, I need to take data that has been
I am starting to learn Play Framework 2, and have met a strange problem.
I have been trying to play around with this for a long time and
i have task which takes a parameter and has three modes of results Example
I have task involving reading SAS .xpt files using .NET. For that I'm using
I have a task to develop an electronic phone book in which i have
I have the task to integrate some commercial sharing stuff into a website. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.