Go Back   Velocity Reviews > Newsgroups > Java
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

Java - Sorting TimeZone

 
Thread Tools Search this Thread
Old 11-03-2009, 12:09 AM   #1
Default Sorting TimeZone


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.

--
Wojtek




Wojtek
  Reply With Quote
Old 11-03-2009, 12:22 AM   #2
markspace
 
Posts: n/a
Default Re: Sorting TimeZone
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.
>



I guess put them all in an array, the use Array.sort( Object[],
Comparator<T>) to sort them as you desire.



markspace
  Reply With Quote
Old 11-03-2009, 12:48 AM   #3
Wojtek
 
Posts: n/a
Default Re: Sorting TimeZone
markspace wrote :
> 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.
>>

>
>
> I guess put them all in an array, the use Array.sort( Object[],
> Comparator<T>) to sort them as you desire.


Yes, I made my own custom Comparator.

But I just found out what the problem is. You can retrieve a TimeZone
using a variety of names, but that TimeZone only has one display name.
For instance, in Canada:

Canada/Newfoundland Newfoundland Standard Time
Canada/Atlantic Atlantic Standard Time
Canada Eastern Eastern Standard Time
Canada/Central Central Standard Time *
Canada/East-Saskatchewan Central Standard Time *
Canada/Saskatchewan Central Standard Time *
Canada/Mountain Mountain Standard Time
Canada/Pacific Pacific Standard Time *
Canada/Yukon Pacific Standard Time *

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

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

--
Wojtek




Wojtek
  Reply With Quote
Old 11-03-2009, 12:51 AM   #4
markspace
 
Posts: n/a
Default Re: Sorting TimeZone
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() );
}

}


markspace
  Reply With Quote
Old 11-03-2009, 01:06 AM   #5
markspace
 
Posts: n/a
Default Re: Sorting TimeZone
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.



markspace
  Reply With Quote
Old 11-03-2009, 01:17 AM   #6
Lew
 
Posts: n/a
Default Re: Sorting TimeZone
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


Lew
  Reply With Quote
Old 11-03-2009, 03:13 AM   #7
Roedy Green
 
Posts: n/a
Default Re: Sorting TimeZone
On Mon, 02 Nov 2009 16:09:42 -0800, Wojtek <> 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.


Roedy Green
  Reply With Quote
Old 11-03-2009, 05:56 AM   #8
Lew
 
Posts: n/a
Default Re: Sorting TimeZone
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


Lew
  Reply With Quote
Old 11-03-2009, 03:56 PM   #9
Wojtek
 
Posts: n/a
Default Re: Sorting TimeZone
Lew wrote :
> 'TimeZone' can easily be a map key, yes, even for a 'TreeMap'.


No it cannot be used as a key. It does not have a 'compareTo' method.

--
Wojtek




Wojtek
  Reply With Quote
Old 11-03-2009, 04:57 PM   #10
Lew
 
Posts: n/a
Default Re: Sorting TimeZone
Lew wrote :
>> 'TimeZone' can easily be a map key, yes, even for a 'TreeMap'.

>


Wojtek wrote:
> No it cannot be used as a key. It does not have a 'compareTo' method.
>


Yes, it can be used as a key!
<http://java.sun.com/javase/6/docs/ap...p.html#TreeMap
(java.util.Comparator)>

The Javadocs are your friend.

--
Lew




Lew
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Outlook and sorting of messages Rolf Computer Support 6 10-12-2008 10:30 PM
Re: Sorting Function fdg Computer Support 0 12-12-2005 10:01 AM
Re: Sorting Function fdg Computer Support 0 12-12-2005 09:57 AM
Sorting Function JSolesky@aol.com Computer Support 4 12-09-2005 06:19 PM
Any dedicated freeware text sorting utility for Windows? (besides Sorter from Aldra) smic Computer Support 1 08-18-2003 06:20 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46