Object sorting Using Comparator interface................................
//User.java
//UidComparator.java
//NameComparator.java
//UserMain.java
==============================================================================================
package collectctiondemo;
public class User {
int uid;
String uname;
String uaddress;
float salary;
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUaddress() {
return uaddress;
}
public void setUaddress(String uaddress) {
this.uaddress = uaddress;
}
public float getSalary() {
return salary;
}
public void setSalary(float salary) {
this.salary = salary;
}
}
===========================================================================
package collectctiondemo;
import java.util.Comparator;
public class UidComparator implements Comparator<User>{
@Override
public int compare(User u1, User u2) {
if(u1.getUid()>u2.getUid())
return 1;
else if(u1.getUid()<u2.getUid())
return -1;
else
return 0;
}
}
================================================================================
package collectctiondemo;
import java.util.Comparator;
public class NameComparator implements Comparator<User> {
@Override
public int compare(User u1, User u2) {
return u1.getUname().compareTo(u2.getUname());
}
}
===============================================================================
package collectctiondemo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class UserMain {
public static void main(String[] args) {
List<User> l=new ArrayList<User>();
User u1=new User();
u1.setUid(5);
u1.setUname("ballabh");
u1.setUaddress("madhubani");
u1.setSalary(26000.00f);
l.add(u1);
User u2=new User();
u2.setUid(3);
u2.setUname("govind");
u2.setUaddress("darbhangha");
u2.setSalary(16000.00f);
l.add(u2);
User u3=new User();
u3.setUid(4);
u3.setUname("mani");
u3.setUaddress("motihari");
u3.setSalary(48000.00f);
l.add(u3);
User u4=new User();
u4.setUid(6);
u4.setUname("ajit");
u4.setUaddress("patna");
u4.setSalary(59000.00f);
l.add(u4);
User u5=new User();
u5.setUid(9);
u5.setUname("sanjay");
u5.setUaddress("bhagalpur");
u5.setSalary(18000.00f);
l.add(u5);
System.out.println("---------------Before Sorting--------------");
for(User u:l)
{
System.out.println(u.getUid()+"-----"+u.getUname()+"-----"+u.getUaddress()+"-----"+u.getSalary());
}
Collections.sort(l,new UidComparator());
//Collections.sort(l,new NameComparator());
System.out.println("----------------After Sorting-----------------");
for(User u:l)
{
System.out.println(u.getUid()+"-----"+u.getUname()+"-----"+u.getUaddress()+"-----"+u.getSalary());
}
}
}
o/p:-------------------------
---------------Before Sorting--------------
5-----ballabh-----madhubani-----26000.0
3-----govind-----darbhangha-----16000.0
4-----mani-----motihari-----48000.0
6-----ajit-----patna-----59000.0
9-----sanjay-----bhagalpur-----18000.0
----------------After Sorting-----------------
3-----govind-----darbhangha-----16000.0
4-----mani-----motihari-----48000.0
5-----ballabh-----madhubani-----26000.0
6-----ajit-----patna-----59000.0
9-----sanjay-----bhagalpur-----18000.0
//User.java
//UidComparator.java
//NameComparator.java
//UserMain.java
==============================================================================================
package collectctiondemo;
public class User {
int uid;
String uname;
String uaddress;
float salary;
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUaddress() {
return uaddress;
}
public void setUaddress(String uaddress) {
this.uaddress = uaddress;
}
public float getSalary() {
return salary;
}
public void setSalary(float salary) {
this.salary = salary;
}
}
===========================================================================
package collectctiondemo;
import java.util.Comparator;
public class UidComparator implements Comparator<User>{
@Override
public int compare(User u1, User u2) {
if(u1.getUid()>u2.getUid())
return 1;
else if(u1.getUid()<u2.getUid())
return -1;
else
return 0;
}
}
================================================================================
package collectctiondemo;
import java.util.Comparator;
public class NameComparator implements Comparator<User> {
@Override
public int compare(User u1, User u2) {
return u1.getUname().compareTo(u2.getUname());
}
}
===============================================================================
package collectctiondemo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class UserMain {
public static void main(String[] args) {
List<User> l=new ArrayList<User>();
User u1=new User();
u1.setUid(5);
u1.setUname("ballabh");
u1.setUaddress("madhubani");
u1.setSalary(26000.00f);
l.add(u1);
User u2=new User();
u2.setUid(3);
u2.setUname("govind");
u2.setUaddress("darbhangha");
u2.setSalary(16000.00f);
l.add(u2);
User u3=new User();
u3.setUid(4);
u3.setUname("mani");
u3.setUaddress("motihari");
u3.setSalary(48000.00f);
l.add(u3);
User u4=new User();
u4.setUid(6);
u4.setUname("ajit");
u4.setUaddress("patna");
u4.setSalary(59000.00f);
l.add(u4);
User u5=new User();
u5.setUid(9);
u5.setUname("sanjay");
u5.setUaddress("bhagalpur");
u5.setSalary(18000.00f);
l.add(u5);
System.out.println("---------------Before Sorting--------------");
for(User u:l)
{
System.out.println(u.getUid()+"-----"+u.getUname()+"-----"+u.getUaddress()+"-----"+u.getSalary());
}
Collections.sort(l,new UidComparator());
//Collections.sort(l,new NameComparator());
System.out.println("----------------After Sorting-----------------");
for(User u:l)
{
System.out.println(u.getUid()+"-----"+u.getUname()+"-----"+u.getUaddress()+"-----"+u.getSalary());
}
}
}
o/p:-------------------------
---------------Before Sorting--------------
5-----ballabh-----madhubani-----26000.0
3-----govind-----darbhangha-----16000.0
4-----mani-----motihari-----48000.0
6-----ajit-----patna-----59000.0
9-----sanjay-----bhagalpur-----18000.0
----------------After Sorting-----------------
3-----govind-----darbhangha-----16000.0
4-----mani-----motihari-----48000.0
5-----ballabh-----madhubani-----26000.0
6-----ajit-----patna-----59000.0
9-----sanjay-----bhagalpur-----18000.0
No comments:
Post a Comment