Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Object Sorting

Posted on 2011-02-24
8
Medium Priority
?
428 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 1000 total points
ID: 34977269
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 1000 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Suggested Courses

610 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