sort arrays in loops

3 views
Skip to first unread message

Mic (BeeBole)

unread,
Dec 24, 2009, 11:32:06 AM12/24/09
to JavaScript Templates Engine PURE
Hi,
I've added a sort functionality for iterations (only on arrays, not
object collections).
The array is sorted before sending it to the rendering engine through
a JS function.
It is a classical function as you can use with Array.sort( fn ).

Here's an example, on how to use it:
directive:{
'tbody tr':{
'player<-players':{
'td':'player'
},
sort:function(a, b){
return a > b ? 1 : -1;
}
}
}

Download the revision 2.30 from http://github.com/pure/pure
And check the example 4 in the examples page /index.html

It works well in our app, but please try it.
Feedback, ideas for other utility, suggestions are welcome.
Cheers,

Mic

Brian

unread,
Jan 16, 2010, 4:39:22 AM1/16/10
to JavaScript Templates Engine PURE
I've used this, and it works wonderfully!

On Dec 24 2009, 8:32 am, "Mic (BeeBole)" <tch...@gmail.com> wrote:
> Hi,
> I've added a sort functionality for iterations (only on arrays, not
> object collections).
> The array is sorted before sending it to the rendering engine through
> a JS function.
> It is a classical function as you can use with Array.sort( fn ).
>
> Here's an example, on how to use it:
> directive:{
>         'tbody tr':{
>                 'player<-players':{
>                         'td':'player'
>                 },
>                 sort:function(a, b){
>                         return a > b ? 1 : -1;
>                 }
>         }
>
> }
>

> Download the revision 2.30 fromhttp://github.com/pure/pure

Mic (BeeBole)

unread,
Feb 3, 2010, 10:54:05 AM2/3/10
to JavaScript Templates Engine PURE
Thanks, at least some recognition ;)

Brian Gates

unread,
Feb 3, 2010, 1:44:14 PM2/3/10
to pure-unobtrusive...@googlegroups.com
Haha, yes, you're doing some fantastic work =)

--
You received this message because you are subscribed to the Google Groups "JavaScript Templates Engine PURE" group.
To post to this group, send email to Pure-Unobtrusive...@googlegroups.com.
To unsubscribe from this group, send email to Pure-Unobtrusive-Rende...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/Pure-Unobtrusive-Rendering-Engine?hl=en.


tjwise

unread,
Feb 4, 2010, 2:26:00 PM2/4/10
to JavaScript Templates Engine PURE
Funny, I was going to ask about a feature like this. Further, filter
and transform would be terrific additions. I find in a number of
cases that I preceed the render with essentially the same loops in
order to build a runtime list of items I wish to render. If I could
embed that filter directly in the iterator, like sort, that would be a
very strong feature. I would call the filter before the sort, so if
there is any significant data reduction, the sort will have less
work. Then, I would call transform to build a new object. Sort would
work on this final result. And, like the iterator, I would use the
same context for the passed argument.

Here's an example, on how to use it:
directive:{
'tbody tr':{
'player<-players':{
'td':'player'
},
sort:function(a, b){
return a > b ? 1 : -1;

},
filter: function(context) {
return context.item.age < 18;
},
transform: function(context) {
return {
abbreviatedName:
context.item.firstName.charAt(0) + ' ' + context.item.lastName,
roomMate:
determineRoomMatePreference(context.item).fullName
}
}
}
}

On Dec 24 2009, 11:32 am, "Mic (BeeBole)" <tch...@gmail.com> wrote:
> Hi,
> I've added a sort functionality for iterations (only on arrays, not
> object collections).
> The array is sorted before sending it to the rendering engine through
> a JS function.
> It is a classical function as you can use with Array.sort( fn ).
>
> Here's an example, on how to use it:
> directive:{
>         'tbody tr':{
>                 'player<-players':{
>                         'td':'player'
>                 },
>                 sort:function(a, b){
>                         return a > b ? 1 : -1;
>                 }
>         }
>
> }
>

> Download the revision 2.30 fromhttp://github.com/pure/pure

Mic (BeeBole)

unread,
Feb 5, 2010, 6:32:17 AM2/5/10
to JavaScript Templates Engine PURE
Not sure about the utility of a "transform" property.
sort and filter are ok for loops, but then the way to handle the data,
depends on other sub selectors(or the '.' if you want to fill the TD).

The filter idea is great! When done in pure.js, it will save me some
code too.
Thanks,

tjwise

unread,
Feb 5, 2010, 12:34:56 PM2/5/10
to JavaScript Templates Engine PURE
Tis true that Transform is not as necessary. You can do any transform
operation as a function on the node, instead of a simple render
directive.

BTW: it is Pure Awesomeness that you have wrought.

> > > Mic- Hide quoted text -
>
> - Show quoted text -

Mic (BeeBole)

unread,
Feb 9, 2010, 3:16:11 PM2/9/10
to JavaScript Templates Engine PURE
The filter is available from the revision 2.35
The documentation is there: http://beebole.com/pure/documentation/iteration-with-directives/
Thanks tjwise for the idea, I've already used it in our app!
Reply all
Reply to author
Forward
0 new messages