0

Background: Im creating this Employee Records application, where you can add a persons ID, First Name, Last Name, Salary and Start Date. It consists of a "Add" button, "Remove" button, "List" Combo box, and an output.

What I need help with is sorting the arrayList by ID and displaying the sorted version in the output. `public class EmployeeRecordsView extends FrameView{

    ArrayList <one> myList = new ArrayList <one>();
    //List<one> records = new ArrayList<one>();
    String iD, first, last, salary, startDate;
    public EmployeeRecordsView(SingleFrameApplication app) {
      //Cut out code
    }

    class one {
        String iD, first, last, salary, startDate;
        one (String _iD, String _first, String _last, String _salary, String
        _startDate){
            iD = _iD;
            first = _first;
            last = _last;
            salary = _salary;
            startDate = _startDate;
        }
    }
    private void btnAddMouseClicked(java.awt.event.MouseEvent evt) {                                    
       one emp;
       iD = id.getText();
       first = firstName.getText();
       last = lastName.getText();
       salary = sal.getText();
       startDate = start.getText();
       emp = new one(iD, first, last, salary, startDate);
       myList.add(emp);
    }                                   

    private void btnRemoveMouseClicked(java.awt.event.MouseEvent evt) {                                       
        String remove;
        remove = id.getText();
        myList.remove(remove);
    }                                      

    private void btnExitMouseClicked(java.awt.event.MouseEvent evt) {                                     
        System.exit(0);
    }                                    


    private void jComboBox1MouseClicked(java.awt.event.MouseEvent evt) {
     if (jComboBox1.getSelectedItem() == "Order of Addition"){
            String temp="";

        for (int x=0; x<=myList.size()-1; x++) {
            temp = temp + "ID#: " + myList.get(x).iD + ", "
                    + "First Name: " + myList.get(x).first + ", "
                    + "Last Name: " + myList.get(x).last + ", "
                    + "Annual Salary: " + myList.get(x).salary + ", "
                    + "Starting Date: " + myList.get(x).startDate + "\n";
        }
        outPut.setText(temp);
        }

     if (jComboBox1.getSelectedItem() == "ID"){
         String temp="";
        for (int x=0; x<=myList.size()-1; x++) {
                  temp = temp + "ID#: " + myList.get(x).iD + ", "
                    + "First Name: " + myList.get(x).first + ", "
                    + "Last Name: " + myList.get(x).last + ", "
                    + "Annual Salary: " + myList.get(x).salary + ", "
                    + "Starting Date: " + myList.get(x).startDate + "\n";
        }
        outPut.setText(temp);
     }`
    }
14
  • Collections.sort(List, Comparator) Commented Apr 30, 2014 at 1:02
  • @MadProgrammer Where do I put that? Commented Apr 30, 2014 at 1:05
  • When do you want to sort the list? Commented Apr 30, 2014 at 1:06
  • 1
    Conor that makes no sense at all. Just because you can do something doesn't mean that you should do something. Except for read a tutorial -- you definitely should read the Swing JButton tutorial before making more statements like that. Commented Apr 30, 2014 at 1:14
  • 1
    @ConorWatt The read the tutorials to understand the event handlers you should be using. It is recommended to use ActionListener for buttons, not MouseListener, as these will be notified when the user presses [Enter] (and on some look and feels [Space]) as will as when the click it with the mouse Commented Apr 30, 2014 at 1:19

1 Answer 1

2

Use Collections.sort(List, Comparator), which will allow you to sort a List in a customised manner through the use of the Comparator.

So, based on a slight modification to you one class, to included a getter to retrieve the iD value

class One {

    String iD, first, last, salary, startDate;

    One(String _iD, String _first, String _last, String _salary, String _startDate) {
        iD = _iD;
        first = _first;
        last = _last;
        salary = _salary;
        startDate = _startDate;
    }

    public String getID() {
        return iD;
    }
}

You could use...

Collections.sort(myList, new Comparator<One>() {
    @Override
    public int compare(One o1, One o2) {
        return o1.getID().compareTo(o2.getID());
    }
});

Now note, this will sort the ID's in natural order, that is 1 will not appear before 10, this is how String sorting works...

Sign up to request clarification or add additional context in comments.

4 Comments

One the very first line would I write implements Comparator <one> beside extends FrameView>?
@ConorWatt: no, don't have your GUI implement Comparator.
Would I put Collections.sort(myList, new Comparator<one>() { public int compare(one o1, one o2) { return o1.getID().compareTo(o2.getID()); } }); Inside or outside the last for statment?
You should put it where you want the list to be sorted, presumably after it's been updated and before you want to display the results

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.