![]() |
|
|
|
#1 |
|
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 |
|
|
|
|
#2 |
|
Posts: n/a
|
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 |
|
|
|
#3 |
|
Posts: n/a
|
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 |
|
|
|
#4 |
|
Posts: n/a
|
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 |
|
|
|
#5 |
|
Posts: n/a
|
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 |
|
|
|
#6 |
|
Posts: n/a
|
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 |
|
|
|
#7 |
|
Posts: n/a
|
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 |
|
|
|
#8 |
|
Posts: n/a
|
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 |
|
|
|
#9 |
|
Posts: n/a
|
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 |
|
|
|
#10 |
|
Posts: n/a
|
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 |
|
![]() |
| Thread Tools | Search this Thread |
|
|
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 |