Is there a descending sorted class?

R

RC

I searched java.util.* package, all I found are
ascending sorted, such as SortedMap, SortedSet,
TreeMap, TreeSet.

I wouldn't found descending sorted.
Currently I made descending sorted by TreeMap then
push the keys into Stack, then pop
the keys back.
Anyone has better idea?
 
R

Robert Klemme

RC said:
I searched java.util.* package, all I found are
ascending sorted, such as SortedMap, SortedSet,
TreeMap, TreeSet.

I wouldn't found descending sorted.
Currently I made descending sorted by TreeMap then
push the keys into Stack, then pop
the keys back.
Anyone has better idea?

The proper way is to create a custom Comparator implementation that
yields the order you need.

robert
 
T

TechBookReport

RC said:
I searched java.util.* package, all I found are
ascending sorted, such as SortedMap, SortedSet,
TreeMap, TreeSet.

I wouldn't found descending sorted.
Currently I made descending sorted by TreeMap then
push the keys into Stack, then pop
the keys back.
Anyone has better idea?

You can supply your own comparator when you create your Map, this can
override the default ascending comparator. Take a look at the Comparator
interface.
 
T

Thomas Hawtin

RC said:
I wouldn't found descending sorted.
Currently I made descending sorted by TreeMap then
push the keys into Stack, then pop
the keys back.

You can supply a comparator to the TreeMap that reverse the natural
order. Unsurprisingly this is quite common, so one is provided in
Collections.reverseOrder(). Also there is a method to create comparator
that runs into the reverse order to one supplied.

http://download.java.net/jdk6/docs/api/java/util/Collections.html#reverseOrder()

If your data is already in a List, then you can sort it in place with
Collections.sort.

http://download.java.net/jdk6/docs/api/java/util/Collections.html#sort(java.util.List,
java.util.Comparator)

Tom Hawtin
 
C

Chris Smith

RC said:
I searched java.util.* package, all I found are
ascending sorted, such as SortedMap, SortedSet,
TreeMap, TreeSet.

I wouldn't found descending sorted.
Currently I made descending sorted by TreeMap then
push the keys into Stack, then pop
the keys back.
Anyone has better idea?

public class ReverseComparator<T> implements Comparator<T>
{
private Comparator<T> ascending;

public ReverseComparator(Comparator<T> asc)
{
this.ascending = asc;
}

public int compare(T a, T b)
{
int asc = ascending.compare(a, b);

if (asc == Integer.MIN_VALUE) return Integer.MAX_VALUE;
else return -asc;
}
}

public class NaturalComparator<T extends Comparable<T>>
implements Comparator<T>
{
public ReverseComparator()
{
}

public int compare(T a, T b)
{
int asc = a.compareTo(b);

if (asc == Integer.MIN_VALUE) return Integer.MAX_VALUE;
else return -asc;
}
}

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
C

Chris Smith

Chris Smith said:
public class ReverseComparator<T> implements Comparator<T>
public class NaturalComparator<T extends Comparable<T>>
implements Comparator<T>

Oops. Read Thomas's reply, not mine.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
L

Lasse Reichstein Nielsen

Chris Smith said:
int asc = ascending.compare(a, b);

if (asc == Integer.MIN_VALUE) return Integer.MAX_VALUE;
else return -asc;

or just:
int asc = ascending.compare(b,a);

/L :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top