Solved

how to sort objects ascending and descending

Posted on 2006-06-21
8
415 Views
Last Modified: 2013-11-23
How would you use comparators to sort objects ascending and descending.

Suppose I want to be able to sort an object by the age field either ascending or descending.
Also I have an object that can be sorted by more than field. You can sort by name and age.
How can I implement more than 1 comparator?

0
Comment
Question by:inzaghi
[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
  • 3
  • 3
  • 2
8 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 250 total points
ID: 16949833
You can pass the field as a parameter to the ctor of the Comparator. Reverse the comparison for asc.ssec
0
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 16949836
You would use a different Comparator for each sort order that you needed.
To get the reverse order iuse:

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Collections.html#reverseOrder(java.util.Comparator)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16949837
Typo:

>>Reverse the comparison for asc.ssec

should have been

Reverse the comparison for asc/desc
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

Author Comment

by:inzaghi
ID: 16949893
I am using java 1.4
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16950065
:-)

You only need one Comparator
0
 

Author Comment

by:inzaghi
ID: 16950270
Alternatively I could have one class which implements the Comparable interface and otehr comparators I can define as annonymous classes

 public static final Comparator AGE_ORDER = new Comparator() {
             public int compare(Object o1, Object o2) {
             }
 };

 public static final Comparator NAME_ORDER = new Comparator() {
             public int compare(Object o1, Object o2) {
             }
};
0
 
LVL 92

Expert Comment

by:objects
ID: 16950328
> Alternatively I could have one class which implements the Comparable interface and otehr comparators I can define as annonymous classes

yes you could use inner classes for your comparators. It would still give you a seperate class for each sort order you needed, ie. one comparator per sort order.

0

Featured Post

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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 learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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 …

634 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