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

  • Home
  • SEARCH
  • 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 3230944
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:59:12+00:00 2026-05-17T16:59:12+00:00

Basically, I have a Container class called Employees which has in it an ArrayList.

  • 0

Basically, I have a Container class called “Employees” which has in it an ArrayList. This ArrayList contains “Employee” objects, which in turn contain “EmployeeData” objects which in turn contain String objects such as “first” or “last” (which are employee names).

Here’s a diagram of the ArrayList structure:

ArrayList[Employee] emps ==> 1:Many ==> Employee emp
Employee emp ==> 1:1 ==> EmployeeData data
EmployeeData data ==> 1:2 ==> String last // A string that contains employee's last name.

How in the world would I perform a quicksort on the ArrayList so that the “Employee” objects in it are in alphabetical order based on the String object “last”? It seems kinda complicated!


Here’s a basic design of my classes:

class Employees{
    //data:
        private ArrayList<Employee> emps = new ArrayList<Employee>();

    //Some constructors go here

    //Methods to add, remove, toString, etc, go here

    public /*output a sorted ArrayList?*/ sort(){
        // Some kind of "quicksort" in here to modify or create a new ArrayList sorted by employee's las name...
    }
}

class Employee{
    //data:
    EmployeeData data;
    // Some methods to construct and modify EmployeeData data.
}

class EmployeeData{
    //data:
        String first, last; // I wish to sort with "last". How do you do it?
        double payrate, hours;
    //...methods...
}

As you can see, those are the classes. I have no idea how to implement “sort” in the “Employees” class so that it sorts the ArrayList by the “last” variable of the “EmployeeData” class.

  • 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-17T16:59:13+00:00Added an answer on May 17, 2026 at 4:59 pm

    The best practice is to encapsulate the sorting logic in the class stored in the ArrayList, Employee in this case. Implement Comparable by creating a compareTo(Employee) method.

    import java.util.*;
    
    public class Employee  implements Comparable<Employee> {
        public EmployeeData Data;
    
        public Employee(String first, String last)
        {
            Data = new EmployeeData(first, last);
        }
    
        public int compareTo(Employee other)
        {
            return Data.Last.compareTo(other.Data.Last);
        }
    
        public String toString() {
            return Data.First + " " + Data.Last;
        }
    
        public static void main(String[] args) throws java.io.IOException {
            ArrayList list = new ArrayList();
            list.add(new Employee("Andy", "Smith"));
            list.add(new Employee("John", "Williams"));
            list.add(new Employee("Bob", "Jones"));
            list.add(new Employee("Abraham", "Abrams"));
            Collections.sort(list);
            for (int i = 0; i < list.size(); i++)
            {
                System.out.println(list.get(i));
            }
            System.in.read();
        }
    }
    
    public class EmployeeData {
        public String First;
        public String Last;
        public EmployeeData(String first, String last)
        {
            First = first;
            Last = last;
        }
    }
    

    Output:

    Abraham Abrams
    Bob Jones
    Andy Smith
    John Williams
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I basically have a page which shows a processing screen which has been flushed
Basically I have the following class: class StateMachine { ... StateMethod stateA(); StateMethod stateB();
Basically you have two ways for doing this: for (int x = 0; x
I basically have the following flow: XML -> JSON -> Spring MVC -> jsp
I am looking in to ways to enable a site to basically have something
I would like to do the following. Basically have a stored procedure call another
Basically I have some code to check a specific directory to see if an
Basically I have a bunch of unmanaged VC++ static libraries. And the VC++ GUI
Basically I have a website. I have a properly setup sitemap so I assume
Basically I have a small template that looks like: <xsl:template name=templt> <xsl:param name=filter />

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.