Sorting an ArrayList that contains pojo class:
Please go through the below example.
First create a new java class and named as Employee.
package com.sana.collections;
public class Employee {
int id;
String firstName;
String lastName;
public Employee(int id, String firstName, String lastName) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return "Employee{" + "id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + '}';
public class Employee {
int id;
String firstName;
String lastName;
public Employee(int id, String firstName, String lastName) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return "Employee{" + "id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + '}';
// return firstName ;
}
}
}
}
Actual Class for Testing:
Create a new java class named as SortListTest:
package com.sana.collections;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
class MyComparator implements Comparator<Employee> {
public int compare(Employee emp1, Employee emp2) {
// comparing Firstname from both objects @Sorting based on First Name
return emp1.getFirstName().compareTo(emp2.getFirstName());
}
}
public class SortListTest {
public static void main(String[] args) {
Employee emp1 = new Employee(1, "SHEKAR", "SANA");
Employee emp2 = new Employee(2, "CHIRU", "ALLEM");
Employee emp3 = new Employee(3, "CHANDRA", "SHEKAR");
Employee emp4 = new Employee(4, "Test", "Test");
Employee emp5 = new Employee(4, "Mahesh", "Burugu");
List<Employee> employeeList = new ArrayList<Employee>();
employeeList.add(emp1);
employeeList.add(emp2);
employeeList.add(emp3);
employeeList.add(emp4);
employeeList.add(emp5);
Collections.sort(employeeList, new MyComparator());
for (Employee e : employeeList) {
System.out.println(e);
}
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
class MyComparator implements Comparator<Employee> {
public int compare(Employee emp1, Employee emp2) {
// comparing Firstname from both objects @Sorting based on First Name
return emp1.getFirstName().compareTo(emp2.getFirstName());
}
}
public class SortListTest {
public static void main(String[] args) {
Employee emp1 = new Employee(1, "SHEKAR", "SANA");
Employee emp2 = new Employee(2, "CHIRU", "ALLEM");
Employee emp3 = new Employee(3, "CHANDRA", "SHEKAR");
Employee emp4 = new Employee(4, "Test", "Test");
Employee emp5 = new Employee(4, "Mahesh", "Burugu");
List<Employee> employeeList = new ArrayList<Employee>();
employeeList.add(emp1);
employeeList.add(emp2);
employeeList.add(emp3);
employeeList.add(emp4);
employeeList.add(emp5);
Collections.sort(employeeList, new MyComparator());
for (Employee e : employeeList) {
System.out.println(e);
}
}
}
OUTPUT:
Employee{id=3, firstName=CHANDRA, lastName=SHEKAR}
Employee{id=2, firstName=CHIRU, lastName=ALLEM}
Employee{id=4, firstName=Mahesh, lastName=Burugu}
Employee{id=1, firstName=SHEKAR, lastName=SANA}
Employee{id=4, firstName=Test, lastName=Test}
Employee{id=2, firstName=CHIRU, lastName=ALLEM}
Employee{id=4, firstName=Mahesh, lastName=Burugu}
Employee{id=1, firstName=SHEKAR, lastName=SANA}
Employee{id=4, firstName=Test, lastName=Test}
BY
SANA
Excellent post...keep it up
ReplyDeleteThanks Buddy...
ReplyDeleteThe fitness tips for motivation we've been discussing can be very powerful if you use them. The second option is to use headphones as you probably did at the gym in the past. Visit a Dermatologist: If you are facing any chronic skin problem like pimples, acne and blemishes then consult a doctor at first place before going to a beautician.
ReplyDeleteMy homepage; just click the following document
Many websites are involved in the funny picture concept and they include the cat playing
ReplyDeletewith the plaything or with the trainer. Men2hire is the best
friendship, companionship & London Escorts Agency available for online dating
and booking. simply keep in mind few websites that may lead you to fun and
smiles on face.
Feel free to visit my site: lol pictures
Asking questions are genuinely nice thing if you are not understanding anything totally, however this piece of writing gives pleasant understanding even.
ReplyDeletemy web page: Biotechnology Today
A lot of us who are under constant pressure tend
ReplyDeleteto eat too much and do not take good care of their bodies.
Then you'll have no trouble motivating yourself to do it. Stretching will help you prevent injuries and it will help you loosen up your muscles.
Here is my blog: fitness tips and workouts
Hi there just wanted to give you a brief heads up and let you know a few of
ReplyDeletethe pictures aren't loading properly. I'm not
sure why but I think its a linking issue.
I've tried it in two different web browsers and both show the same results.
my blog post; Highly recommended Site
you'll be able to choose out your funny image as per your want and interest. This trend brings more enthusiast people to upload the beautiful funny videos to share with others and get their precious feedback. Use old fabric to create puppets, monsters, and any other creatures that your child can imagine.
ReplyDeleteAlso visit my web-site ... phpfox.webtower.org
Hello, I check your blog on a regular basis. Your humoristic style is witty, keep doing
ReplyDeletewhat you're doing!
My web site: http://prettylittleliarss03e18.xitti.com/link/778
Moderate to strong winds blowing from the
ReplyDeletesouthwest to south will prevail over northern
and Central Luzon and coastal waters along these areas
will be moderate to rough. " Earlier today some cruise ship struck land in Alaska. It's more than just doing the right things or being involved in a good opportunity.
Here is my web page: cyberpresidents.com
Moderate to strong winds blowing from the southwest to south will prevail over northern and Central
ReplyDeleteLuzon and coastal waters along these areas will be moderate to rough.
" Earlier today some cruise ship struck land in Alaska. It's more than just doing the right things or being involved in a good opportunity.
Here is my weblog cyberpresidents.com
Based on my own experiences I am going to make five recommendations that I wish someone had made to me to help take the fear out of fitness.
ReplyDeleteWe know because they have done so for millions of other people.
Having some company can also make the fitness session more fun.
Feel free to surf to my weblog asdm.in
Ridiculous story there. What occurred after?
ReplyDeleteGood luck!
Feel free to surf to my website ... simply click the following site
Far too many children become the victims of abuse, neglect, or abandonment and then sadly, often they become wards of the court who will
ReplyDeleteeventually determine their fate. This glass would cost less than $1000 and would be available to
almost all the people in Britain. If a person is connected with the latest headlines, it will
develop an awareness in him with regards to the world which surrounds him.
Also visit my web site; Latest Daily News
After I initially commented I seem to have clicked
ReplyDeleteon the -Notify me when new comments are added- checkbox and from now on every time a comment
is added I get four emails with the exact same comment.
There has to be a means you can remove me from that service?
Cheers!
Stop by my blog post: Biomedical Companies
For most up-to-date information you have to pay a visit the web and on the web I found this web site as
ReplyDeletea most excellent website for newest updates.
Feel free to surf to my web page - http://www.best-moving-quotes.com/love-quotes