I found that in GroupingCollection2 there is a method named buildGroups() where the logic of grouping lies. To solve my issue, I added the code in this method. I did monkey patching for GroupingCollection2.
I added logic which checks if the currentGroup already exists in newCollection or not. If not then only it is added to newCollection otherwise the currentData is added to the existing group.
I have done this but still I am not sure that is this the best way to do this or not.
Please let me know if any other solution is there.