简体   繁体   中英

Java Sorting arrayList based on textField

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);
     }`
    }

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM