Why following sort does not work It does not sort last 2 3
Why following sort does not work? (It does not sort last 2 - 3 numbers)
//Sort order
class SortNumbListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
Collections.sort(list);
for(int i = 0; i < list.size(); i++)
{
textfield2.setText(list.toString());
}
System.out.print(list + \"\ \");
}
}
Solution
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.JTextField;
public class SorAction {
public JTextField textfield2;
public List<Integer> list;
public SorAction() {
// TODO Auto-generated constructor stub
textfield2 = new JTextField();
list = new ArrayList<Integer>();
list.add(20);
list.add(12);
list.add(32);
list.add(3);
new SortNumbListener().actionPerformed(new ActionEvent(\"\", 0, null));
}
public static void main(String[] args) {
try {
new SorAction();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
class SortNumbListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
Collections.sort(list);
textfield2.setText(list.toString());
System.out.print(list + \"\ \");
}
}
}
OUTPUT:
[3, 12, 20, 32]
NOTE: please check the above example, sorting fine, but what is the need of loop,
if you want to add the text to textbox use setText() method of textfield

