RE: The new map syntax

"Mike Weerdenburg - de Bruin" <[email protected]> Fri, 18 Nov 2005 13:32:11 +0100
Newsgroups gmane.comp.java.webmacro.user
Message-ID <200511181336959.SM01172@schuurpcxp>
Hello Nikhil,

 

There is a experimental WhileDirective available in the CVS tree. ;-)

 

I personally think that if you specify #set $l = [b, a, c], you want to loop
trough the map in the order b, a, c and not a random order!

If you want to sort it, then you must use a function or something as you
mentioned.

 

Greetings,

Mike

 

  _____  

Van: [email protected]
[mailto:[email protected]] Namens Nikhil G. Daddikar
Verzonden: vrijdag 18 november 2005 12:54
Aan: [email protected]
Onderwerp: Re: [WebMacro-user] The new map syntax

 

Thanks for your detailed response.

I think the spirit of webmacro has been to be web-designer friendly. (That
is why probably there is no while or break.) I don't think when web
designers specify the new map syntax, they are expecting that the backend
will sort it according to the keys or that the map is in a random order. It
is telling them thet when you specify #set $l = [a, b, c], you may get the
list in a random order. I think it is ok for a designer to call a function
if he/she wants to sort it by key or value rather than messing up the order.

-ngd



Marc Palmer wrote: 

-----Original Message-----
From: "Nikhil G. Daddikar"  <mailto:[email protected]> <[email protected]>
To: [email protected]
Date: Fri, 18 Nov 2005 10:52:24 +0530
Subject: [WebMacro-user] The new map syntax
 
  

I like the new #set $map = { k1: v1, k2:v2 } syntax. The only problem
is 
that it does not use the "List Ordered" Map i.e. when I  want to say 
render a select list  from it, it shows things not in the order in
which 
the map was defined. Is there a way where I can specify the 
implementation class to use? I use JDK 1.5 and  would like to specify 
the LinkedHashMap.
    

 
No, I don't believe this is possible. 
 
I'd say it's generally better not rely on the order of definition of map
elements.
 
If you need ordering you can simply create 2 lists. It's not that pretty
but...
 
Alternatively, and usually this makes sense for "human" use, you would
populate lists etc based on a sort order rather than listed order - so you
can just write a trivial helper to return a sorted list of the keys:
 
public class SortTool
{
    public static final Object sort(Object list)
    {
        List l = ListUtil.toList(list);
        Collections.sort(l);
        return l;        
    }
}
 
...and use it like this:
 
#set $sortedKeys = $Sorter.sort($myMap.keySet().iterator())
 
...of course you can make the usage of this tool a LOT nicer by adding
some more intelligence / overidden methods:
 
public class SortTool
{
    public static final Object sort(Object list)
    {
        List l = ListUtil.toList(list);
        Collections.sort(l);
        return l;        
    }
    
    public static final Object sortKeys(Map map)
    {
        return sort( map.keySet().iterator());        
    }
}
 
...and use it like this:
 
#set $sortedKeys = $Sorter.sortKeys($myMap)
 
 
Cheers
 
 
 
-------------------------------------------------------
This SF.Net email is sponsored by the JBoss Inc.  Get Certified Today
Register for a JBoss Training Course.  Free Certification Exam
for All Training Attendees Through End of 2005. For more info visit:
http://ads.osdn.com/?ad_id=7628
<http://ads.osdn.com/?ad_id=7628&alloc_id=16845&op=click>
&alloc_id=16845&op=click
_______________________________________________
Webmacro-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/webmacro-user