Solved

Object Sorting

Posted on 2011-02-24
8
410 Views
Last Modified: 2012-06-22
import java.util.*;
public class Employee{
Date DOB = new Date(0L);
}

I have different Employee objects with different DOB's. I want to sort Employee object based on DOB field. How to do that? Please suggest with the detailed explanation!!
0
Comment
Question by:sumanthpuvvula
8 Comments
 

Author Comment

by:sumanthpuvvula
ID: 34977229
all Employee Objects are in a ArrayList ..... so I need to sort that arrayList based on the date object in Employee
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34977263
You just override compareTo method for your class Employee and then Collections.sort(ArrayList) will work fo you.
I'll preprae some code for you
0
 
LVL 47

Accepted Solution

by:
for_yan earned 250 total points
ID: 34977269
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 92

Expert Comment

by:objects
ID: 34977290
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34977298

Well, the exmample above in fact uses your case with Employee and dates.
Basically you need to add to your class definition "implements Comparable"
and then add method int compare (Employee e1, Employee e2) as shown below.

That would be enough then after Collections.sort(al);
where al is ArrayList of your employyee, thelist al will become sorted

public class Employee implements Comparable {

    public int compare(Employee e1, Employee e2) {
        int dateCmp = e2.hireDate().compareTo(e1.hireDate());
        if (dateCmp != 0)
            return dateCmp;
        return (e1.number() < e2.number() ? -1 :
                (e1.number() == e2.number() ? 0 : 1));
    }

}



}
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 250 total points
ID: 34978629
>>I want to sort Employee object based on DOB field. How to do that?

If you're using Comparable, you just need
public class Employee implements Comparable<Employee> {
...
	public int compare(Employee e1, Employee e2) {
		return e1.DOB.compareTo(e2.DOB);
	}

}

Open in new window

0
 
LVL 53

Expert Comment

by:Dhaest
ID: 35321641
This question has been classified as abandoned and is being closed as part of the Cleanup Program. See my comment at the end of the question for more details.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
A short article about problems I had with the new location API and permissions in Marshmallow
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now