You can simplify the query:<br><br>query "sortedItems"<br> TreeMap( $values: values() )<br> from accumulate( $f: Fact(),<br> init( TreeMap map = new TreeMap(); ),<br> action( map.put(factKey($f), $f); ),<br>
reverse( map.remove(factKey($f)); ),<br> result( map ) )<br>end<br><br>-W<br><br>
<br><div class="gmail_quote">On 28 November 2011 10:58, Syargey <span dir="ltr"><<a href="mailto:Syargey@tut.by" target="_blank">Syargey@tut.by</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
I don't want to sort facts outside of drools rules. The sorting is part of<br>
business requirements. My opinion is that is not a good thing to keep parts<br>
of business logic in different places.<br>
<br>
I spent some time to figure out how it is possible to sort a collection of<br>
facts and here is my solution:<br>
---------------------------<br>
function String factKey(Fact f)<br>
{<br>
return String.format( "%010d~~%s~~%s", f.getRank(), f.getName(),<br>
f.getValue() );<br>
}<br>
<br>
query sortFactsQuery<br>
$factsMap: HashMap()<br>
from accumulate( $f: Fact(),<br>
init( HashMap map = new HashMap(); ),<br>
action( map.put(factKey($f), $f); ),<br>
reverse( map.remove(factKey($f)); ),<br>
result( map ) )<br>
$sortedKeyList: TreeSet()<br>
from collect( String() from $factsMap.keySet() )<br>
$sortedFacts: List()<br>
from accumulate( $key: String() from $sortedKeyList,<br>
collectList( $factsMap.get( $key ) ) )<br>
end<br>
-----------------------<br>
<font color="#888888"><br>
--<br>
View this message in context: <a href="http://drools.46999.n3.nabble.com/facts-custom-sort-tp3538410p3541805.html" target="_blank">http://drools.46999.n3.nabble.com/facts-custom-sort-tp3538410p3541805.html</a><br>
Sent from the Drools: User forum mailing list archive at Nabble.com.<br>
_______________________________________________<br>
rules-users mailing list<br>
<a href="mailto:rules-users@lists.jboss.org" target="_blank">rules-users@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
</font></blockquote></div><br>