bellow i have pasted the complete code
here i am sorting the elements of collection on the basis of name and age
here user will be given choices that he want to sort by name or by age
problem is that
when i enter the chioce 1 then it works properly but when i enter again choice 2 then it does not work properly same thing happens when i enter first choice 2 and then choice 1
but when i enter only one choice (either 1 or 2)after running the program then it works properly ............... i am not able to understand why it is happening like that please help me
package Nageshwar_Raw;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class ArrayList_sorting {
public static void main(
String[] args)throws IOException {
Emp em1=new Emp("rupesh",21);
Emp em2=new Emp("alok",20);
Emp em3=new Emp("vawesh",25);
Emp em4=new Emp("rakesh",26);
Emp em5=new Emp("banti",15);
Emp em6=new Emp("anup",10);
ArrayList al=new ArrayList();
al.add(em1);
al.add(em2);
al.add(em3);
al.add(em4);
al.add(em5);
al.add(em6);
while(true){
System.out.println("Select your choice \n");
System.out.println("Enter 1 to sort on the basis of name");
System.out.println("Enter 2 to sort on the basis of Age");
System.out.println("Enter 3 to Exit");
BufferedReader br=null;
InputStreamReader ins=new InputStreamReader(System.in);
br=new BufferedReader(ins);
int choice=Integer.parseInt(br.readLine());
switch(choice){
case 1: {
System.out.println("inside case1 before sorting ");
Iterator it=al.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
/*
* sorting on the basis of name using selection sort
*/
int out, in, min;
for(out=0; out<al.size()-1; out++)
{
min = out;
for(in=out+1; in<al.size(); in++){
String st1=((Emp)(al.get(in))).getName();
String st2=((Emp)(al.get(min))).getName();
if((st1.compareTo(st2))<0)
min = in; // we have a new min
// swaping
Object obj=al.get(out);
al.set(out,al.get(min));
al.set(min, obj);
}
} // end for(out)
System.out.println("\n inside case1 After sorting");
Iterator it1=al.iterator();
while(it1.hasNext()){
System.out.println(it1.next());
}
break;
}
case 2:{
System.out.println("inside case2 before sorting ");
Iterator it2=al.iterator();
while(it2.hasNext()){
System.out.println(it2.next());
}
/*
* sorting on the basis of Age using selection sort
*/
int out, in, min;
for(out=0; out<al.size()-1; out++) // outer loop
{
min = out; // minimum
for(in=out+1; in<al.size(); in++){ // inner loop
int st1=((Emp)(al.get(in))).getAge();
int st2=((Emp)(al.get(min))).getAge();
if(st1<st2) // if min greater,
min = in; // we have a new min
// swaping
Object obj=al.get(out);
al.set(out,al.get(min));
al.set(min, obj);
}
} // end for(out)
System.out.println("\n inside case2 After sorting");
Iterator it3=al.iterator();
while(it3.hasNext()){
System.out.println(it3.next());
}
break;
}
case 3:{
Exit(0);
break;
}
default:{
System.out.println("Please Enter correct choice");
}
}// end of switch
}// end of while
}
private static void Exit(int i) {
}
}
class Emp {
private String name;
private int age;
public Emp(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Emp [name=" + name + ", age=" + age + "]";
}
}