Modified optgroup logic to display in proper order

The original implementation had an unintuitive sorting algorithm on
filters that were part of a filter group. This implementation places the
optgroup in the first place it was discovered, preserving the original
order as tightly as possible.
Merge remote-tracking branch 'remotes/prashker/master'