I am using proprietary language in Blaze Advisor (rule enginge). I am looking for an algorithm how to keep only top N items in array by groups formed by specific attribute. As an example there are two arrays:
parrent[0].id = 1
parrent[1].id = 2
And second array:
child[0].parrentId = 1
child[0].result = 3.0
child[1].parrentId = 1
child[1].result = 2.0
child[2].parrentId = 1
child[2].result = 4.0
child[3].parrentId = 1
child[3].result = 6.0
child[4].parrentId = 1
child[4].result = -1.0
child[5].parrentId = 2
child[5].result = 1.0
child[6].parrentId = 2
child[6].result = 16.0
child[7].parrentId = 2
child[7].result = 2.0
child[8].parrentId = 2
child[8].result = -10.0
child[9].parrentId = 2
child[9].result = 5.0
I would like to keep only top three elements for each parrentId
in child
array as indicated by result
attribute. In my language I can do all the basic operations - I can use if/else, while, for, for each constructs, and create new variables. I can sort array asc/desc and get indices of sorted elements. I can remove elements of an array.
For my data I need the following result:
child[0].parrentId = 1
child[0].result = 3.0
child[1].parrentId = 1
child[2].result = 4.0
child[3].parrentId = 1
child[3].result = 6.0
child[6].parrentId = 2
child[6].result = 16.0
child[7].parrentId = 2
child[7].result = 2.0
child[9].parrentId = 2
child[9].result = 5.0