?
Solved

Effective sort in JAVA

Posted on 2001-08-28
7
Medium Priority
?
276 Views
Last Modified: 2008-02-01
What is the most effective way of sorting objects in a vector, based on a field in the object
0
Comment
Question by:bharathr
[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
7 Comments
 
LVL 6

Accepted Solution

by:
kotan earned 200 total points
ID: 6432083
1. Let that objects implement java.lang.Comparable
2. Implement public int compareTo(Object o)
3. Call
        Collections.sort(l);
      l = vector.


Example of the compareTo() implementation,

public class Name implements Comparable {
              private String  firstName, lastName;

              public int compareTo(Object o) {
                  Name n = (Name)o;
                  int lastCmp = lastName.compareTo(n.lastName);
                  return (lastCmp!=0 ? lastCmp :
                          firstName.compareTo(n.firstName));
              }
          }

0
 
LVL 1

Expert Comment

by:wgilster
ID: 6432115
First off, there is a superb generic sorting implementation here in devx.  Download that.
Then make a class that extends your base class and have it implement the java.util.Comparator interface (all you need to override is the equals and compareTo methods for that field you spoke of).  You could call this class MyFieldNameIndex.  Link in the class you downloaded from devx, and if you use one of their sort algorithms, your done!
0
 

Expert Comment

by:yoavdo
ID: 6433099
U can use the ObjectSpace jar: jgl (it is free). main concept is to define a class that implemnts the BinaryPredicate : implemnt an excute method which determine which of the two Objects it recives is bigger, U can define it as U want. Then convert your Vector to an VectorArray (a data type of jgl) and call a static method that gets your VectorArray and the predicate U defined. it sorts the VectorArray U gave. The sort method is 'QuickSort' the preformence is good. If U want a detailed example, make a comment.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 6433762
As indicated in previous posts, the java.util.Collections class implements a mergesort algorithm available through its sort methods. kotan suggested that the objects placed in the collection implement Comparable. This typically works for a homogeneous collection because the sort method demands that "all elements in the list must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the list)." This isn't a problem when all objects in the collection are of the same type. But what to do when the collection is heterogeneous?

Collections provides an alternate sort method which takes a List and a Comparator object. A Comparator object implements the ordering function which is to be applied regardless of whether the object types are themselves Comparables and can better deal with heterogeneous collections because you can code it to expect objects of any type appropriate to your application.

The following JDC articles might be of assistance. The first is about Collections with a short segment on comparing and the second described Comparator and Comparable in more detail with respect to arrays of Objects. The principle is the same for collections.

http://developer.java.sun.com/developer/technicalArticles/Collections/Using/#co
http://developer.java.sun.com/developer/TechTips/1999/tt0923.html#tip2

Best regards,
Jim Cakalic
0
 
LVL 5

Expert Comment

by:vemul
ID: 7755806
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- split points between kotan and jim_cakalic

Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

vemul
Cleanup Volunteer
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 7764381
Good call. Thanks.
0
 

Expert Comment

by:SpideyMod
ID: 7847140
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange

jim_cakalic points for you at:
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20489591.html
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month10 days, 3 hours left to enroll

762 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