Sorting List of Objects in DMN

229 views
Skip to first unread message

Ravi Mangalagiri

unread,
Sep 7, 2021, 12:54:58 PM9/7/21
to Kogito development mailing list
Hi,
I need to sort a list of objects based on a complex compare bkm function.

input: list of objects
bkm function: sortObject(p1, p2) returns 0,1,-1 (just like comparator java method)

my decision:
sort(ctx, list, precedes) work?
what is the context here?


Thanks
Ravi Mangalagiri
703-505-4240 (c)

Matteo Mortari

unread,
Sep 7, 2021, 1:41:04 PM9/7/21
to Kogito development mailing list
Hi Ravi,
without a DMN model is a bit difficult to give you a 100% valid answer, but the sort() function is defined as:

sort( list, precedes )

You say you have a BKM with signature: sortObject(p1, p2) r
in other words a BKM with 2 parameters as input,

You say you have a list of objects, I call it `mylist`, then it could be:

sort( mylist, function(x,y) sortObject(x, y) )
or even just
sort( mylist, sortObject )

where

`mylist` could be:
[ { name : "John", age: 47}, { name : "Alice", age : 21 } ]

and `sortObject( p1, p2)` could be the bkm defined as:
p1.age < p2.age

Hope this helps,
MM

--
You received this message because you are subscribed to the Google Groups "Kogito development mailing list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kogito-developm...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kogito-development/CALg%3DC8MWiXv%3Dc0GEJcjMZ4H7jXSOQcLS%2BQSUGjf6qYDgt6%2BuBg%40mail.gmail.com.


--

Ravi Mangalagiri

unread,
Sep 7, 2021, 1:43:14 PM9/7/21
to Kogito development mailing list
Let me try and post the DMN

Thanks for quick response Matteo

Matteo Mortari

unread,
Sep 7, 2021, 1:54:38 PM9/7/21
to Kogito development mailing list
Glad to help
Please find attached demonstration of the generic use case I mentioned,
works as described: Alice in the `Decision-1` node is before John

Hope this helps,
MM

new-file (37).dmn
Screenshot 2021-09-07 at 19.53.01.png

Ravi Mangalagiri

unread,
Sep 8, 2021, 12:27:09 PM9/8/21
to Kogito development mailing list
Matteo,
I spent a day on this and I am not sure what am I missing. The java sort seems to be working perfectly, while the exact code in dmn does not work.

I tweaked the same example, please take a look at the attached example, 

actual order: 1, 2, 3, 4
expected order: 3, 4, 1, 2

java code order: 3, 4, 1, 2 [as expected]
dmn order : 4, 3, 2, 1 (I tried to code the exact logic implemented in java)


Can you take a look, if the sort bkm function implementation is right?


Record.java
RecordComparatorTest.java
RecordComparator.java
sort.dmn

Matteo Mortari

unread,
Sep 8, 2021, 1:42:19 PM9/8/21
to Kogito development mailing list
Hi Ravi,

you might have inadvertently misunderstood the semantic of the FEEL sort() builtin function, the comparator function works ~like SQL, and should return a boolean.

You can solve your business/domain problem by rewriting your bkm sorting comparator, something ~like: (screenshot attached)

It would return 3-4-1-2 as you expect:

Decision-1: [{id=3, name=Ravi, type=student, age=33, accountBalance=900}, {id=4, name=Henry, type=student, age=26, accountBalance=2500}, {id=1, name=John, type=student, age=25, accountBalance=5000}, {id=2, name=Alice, type=professor, age=47, accountBalance=350}]


Hope this helps!

MM 


sort.dmn
Screenshot 2021-09-08 at 19.38.21.png

Ravi Mangalagiri

unread,
Sep 8, 2021, 2:14:35 PM9/8/21
to Kogito development mailing list
Ok that explains. Great! It works.

Thanks

Matteo Mortari

unread,
Sep 8, 2021, 2:24:33 PM9/8/21
to Kogito development mailing list
Glad to hear that solved it !

Thanks for the confirmation,

MM

Reply all
Reply to author
Forward
0 new messages