File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes best way to sort several sets of data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "best way to sort several sets of data" Watch "best way to sort several sets of data" New topic

best way to sort several sets of data

Celinio Fernandes
Ranch Hand

Joined: Jun 28, 2003
Posts: 549

I need to check several sets of data D1 (dataA, dataB, dataC ...),
D2 (dataD, dataE, dataF ...),
against several criterias: criteria1, criteria2, criteria3, criteria4 etc ...

The objective is to get the unique set of data which satisfies all the crierias OR is the nearest one to the criterias.

So my idea is to affect them a score:
If D1.dataA= criteria1 then I increase D1's score by 1
If D1.dataB= criteria2 then I increase D1's score by 1
and I add all the scores and give D1 a total score,etc

and at the end I select the set Dx which has the biggest score.

what solution do you recommend ?
I am looking for any kind of solution, and information on the implementation as well of course.
Hashmap ? TreeSet ? Comparable ?

If someone has already done something like that, please let me know.

Thanks in advance.

SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCBCD 5
Visit my blog
Thejesh G Nagarathna

Joined: Aug 03, 2005
Posts: 2
For sorting you can use Collections.sort (list, comparator)
where in comparator you can specify the on which you want to compare ( in your case the added score)

<a href="" target="_blank" rel="nofollow">My Blog: TechMag</a>
Celinio Fernandes
Ranch Hand

Joined: Jun 28, 2003
Posts: 549

Does anyone know of a more detailed solution please ?
Stuart Gray
Ranch Hand

Joined: Apr 21, 2005
Posts: 410
As Thejesh said, you can use Collections to store your information. Since you say your data is a set, you might want to use the TreeSet class, which implements both the Set and SortedSet interfaces.

In order to determine how the elements are ordered you will need to either implement the Comparable interface in your classes, or write separate Comparator classes.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
That hint was sufficient to dig out the details. Look into the JavaDoc, find the Collections class, find the sort() method, follow the links to List and Comparator. Everything you need is there!

If your data is really held the way you showed it, so you can do neat tests like "if ( d1.dataA == criteria )" then you have a great start.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Megs Maquito
Ranch Hand

Joined: May 18, 2005
Posts: 84
you could also try googling it "java sort tutorial" if you find it hard to understand the java docs

I'm a Hood Ornament
I agree. Here's the link:
subject: best way to sort several sets of data
It's not a secret anymore!