From: markspace on
Wojtek wrote:
> How would you sort timezones?
>
> I am trying to sort them according to their offset from UTC. I cannot
> use a TreeMap because there are many timezones with the same offset,
> which of course over-writes the previously put timezone.
>
> The preferred sort would be offset, then display name.
>


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package sorttimezone;

import java.util.Arrays;
import java.util.Comparator;
import java.util.TimeZone;

/**
*
* @author Brenden
*/
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
String [] zonesIDs = TimeZone.getAvailableIDs();
TimeZone [] allTZ = new TimeZone[zonesIDs.length];
for( int i=0; i< allTZ.length; i++ ) {
allTZ[i]=TimeZone.getTimeZone( zonesIDs[i] );
}
Arrays.sort( allTZ, new SortByOffsetAndName() );

for( TimeZone zone : allTZ ) {
System.out.println( zone.getDisplayName() + ": "+
zone.getRawOffset()/1000/60/60 );
}
}
}

class SortByOffsetAndName implements Comparator<TimeZone> {

public int compare( TimeZone o1, TimeZone o2 )
{
if( o1.getRawOffset() != o2.getRawOffset() ) {
return o1.getRawOffset() - o2.getRawOffset();
}
return o1.getDisplayName().compareTo( o2.getDisplayName() );
}

}
From: markspace on
Wojtek wrote:

> I retrieve them using the left names, but the .getDisplayName() returns
> the right side. Which throws the sorting out.
>

Instead of using getDisplayName() (which was the same thing I did, in my
example I posted), use getID(), that returns the string on the left in
your table.

public int compare( TimeZone o1, TimeZone o2 )
{
if( o1.getRawOffset() != o2.getRawOffset() ) {
return o1.getRawOffset() - o2.getRawOffset();
}
return o1.getID().compareTo( o2.getID() );
}


> Ok, I can make this work. Funny how actually _asking_ a question can
> lead to an answer...


It works that way for pretty much everyone. ;)

From: Lew on
Wojtek wrote:
> How would you sort timezones?

Do you mean 'java.util.TimeZone'?

> I am trying to sort them according to their offset from UTC. I cannot
> use a TreeMap because there are many timezones with the same offset,
> which of course over-writes the previously put timezone.

There's no "of course" about that if you are using your "preferred sort".

Why would you use any kind of 'Map' rather than a 'Collection'?

> The preferred sort would be offset, then display name.

'TimeZone' can easily be a map key, yes, even for a 'TreeMap'. I fail to see
the problem. You simply compare based on your "preferred sort".

I'd be more concerned about relocation of a key when the offset changes for
DST. But perhaps you're only building this structure for a given moment in
time and that isn't a concern for you.

Regardless, here's uncompiled, off-the-cuff code, using a 'Set' instead of a
'Map':

public class SortedZone
{
private long ref = System.currentTimeMillis();

private final TreeSet <TimeZone> zones =
new TreeSet <TimeZone>
( new Comparator <TimeZone> ()
{
@Override public int compare( TimeZone t0, TimeZone t1 )
{
return
(t0 == null? (t1 == null? 0: -1)
: t1 == null? 1
: t0.getOffset( ref ) > t1.getOffset( ref )? 1
: t0.getOffset( ref ) < t1.getOffset( ref )? -1
: t0.getDisplayName().compareTo( t1.getDisplayName() )
);
}
}
);

// cover methods for the Set ...
}

--
Lew
From: Roedy Green on
On Mon, 02 Nov 2009 16:09:42 -0800, Wojtek <nowhere(a)a.com> wrote,
quoted or indirectly quoted someone who said :

>
>I am trying to sort them according to their offset from UTC. I cannot
>use a TreeMap because there are many timezones with the same offset,
>which of course over-writes the previously put timezone.
>
>The preferred sort would be offset, then display name.

I would create my own TimeZone Object with the fields I need then cook
up a Comparable/Comparator for it.

see http://mindprod.com/applet/comparatorcutter.html
to generate the code.
--
Roedy Green Canadian Mind Products
http://mindprod.com

An example (complete and annotated) is worth 1000 lines of BNF.
From: Lew on
Wojtek quoted or indirectly quoted someone who said :
>> I am trying to sort them according to their offset from UTC. I cannot
>> use a TreeMap because there are many timezones with the same offset,
>> which of course over-writes the previously put timezone.
>>
>> The preferred sort would be offset, then display name.

Roedy Green wrote:
> I would create my own TimeZone Object with the fields I need then cook
> up a Comparable/Comparator for it.

Wojtek was speaking of java.util.TimeZone, judging by the fields he mentioned.

--
Lew