proper sorting of the grouped and lines on template availability+