Note that there are some explanatory texts on larger screens.

plurals
  1. POIn Java, How do you quicksort an ArrayList of objects in which the sorting field is multiple layers deep?
    text
    copied!<p>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).</p> <p>Here's a diagram of the ArrayList structure:</p> <pre><code>ArrayList[Employee] emps ==&gt; 1:Many ==&gt; Employee emp Employee emp ==&gt; 1:1 ==&gt; EmployeeData data EmployeeData data ==&gt; 1:2 ==&gt; String last // A string that contains employee's last name. </code></pre> <p>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!</p> <hr> <p>Here's a basic design of my classes:</p> <pre><code>class Employees{ //data: private ArrayList&lt;Employee&gt; emps = new ArrayList&lt;Employee&gt;(); //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... } </code></pre> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload