sorting custom object using comparator interface



import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import javax.swing.SwingUtilities;


class Employee {
private String name;
private float salary;
private int id;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public float getSalary() {
return salary;
}

public void setSalary(float salary) {
this.salary = salary;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

}

class Id implements Comparator {

@Override
public int compare(Employee o1, Employee o2) {
if (o2.getId() > o1.getId()) {
return 1;
} else if (o2.getId() < o1.getId()) {
return -1;
} else {
return 0;
}

}

}

class Salary implements Comparator {
@Override
public int compare(Employee o1, Employee o2) {

if (o2.getSalary() > o1.getSalary()) {
return 1;
} else if (o2.getSalary() < o1.getSalary()) {
return -1;
} else {
return 0;
}
}
}

public class UiCode extends javax.swing.JFrame {
ArrayList list = new ArrayList();

/** Creates new form UiCode */
public UiCode() {
initComponents();
setData();
setDataTextArea();
}

private void setData() {

Employee e = new Employee();
e.setId(2);
e.setName("raju");
e.setSalary(10000.8f);
Employee e1 = new Employee();
e1.setId(1);
e1.setName("sonu");
e1.setSalary(90000.8f);
Employee e2 = new Employee();
e2.setId(4);
e2.setName("ajay");
e2.setSalary(5000.8f);
list.add(e);
list.add(e1);
list.add(e2);
}

void setDataTextArea() {
String text = "";
for (int i = list.size() - 1; i >= 0; i--) {
Employee e = list.get(i);
String te = e.getId() + "\t" + e.getName() + "\t" + e.getSalary();
text = text + te + "\n";
jTextArea1.setText(text);
}
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
//GEN-BEGIN:initComponents
//
private void initComponents() {

jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jButton3 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jButton1.setText("ID");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jButton2.setText("NAME");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);

jButton3.setText("SALARY");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
getContentPane());
getContentPane().setLayout(layout);
layout
.setHorizontalGroup(layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout
.createSequentialGroup()
.addGroup(
layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout
.createSequentialGroup()
.addContainerGap()
.addComponent(
jScrollPane1,
javax.swing.GroupLayout.DEFAULT_SIZE,
376,
Short.MAX_VALUE))
.addGroup(
layout
.createSequentialGroup()
.addGap(
29,
29,
29)
.addComponent(
jButton1)
.addGap(
71,
71,
71)
.addComponent(
jButton2)
.addGap(
67,
67,
67)
.addComponent(
jButton3)))
.addContainerGap()));
layout.setVerticalGroup(layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING).addGroup(
javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup().addGap(43, 43, 43).addComponent(
jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE,
201, Short.MAX_VALUE).addGap(18, 18, 18).addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton3).addComponent(jButton1)
.addComponent(jButton2)).addContainerGap()));

pack();
}//
//GEN-END:initComponents

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
Salary id = new Salary();
Collections.sort(list, id);
setDataTextArea();
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
Comparator id = new Comparator() {
@Override
public int compare(Employee o1, Employee o2) {

return o2.getName().compareTo(o1.getName());
}
};
Collections.sort(list, id);
setDataTextArea();
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Id id = new Id();
Collections.sort(list, id);
setDataTextArea();
//SwingUtilities.updateComponentTreeUI(jTextArea1);
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new UiCode().setVisible(true);
}
});
}

//GEN-BEGIN:variables
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration//GEN-END:variables

}

No comments: