Solved

Object Sorting

Posted on 2011-02-24
8
408 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This is an explanation of a simple data model to help parse a JSON feed
A short article about problems I had with the new location API and permissions in Marshmallow
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

708 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

12 Experts available now in Live!

Get 1:1 Help Now