Fixing invalid grouping by devices

Some dodgy sorting was screwing up the grouping if you selected group by
devices. This is now fixed.