Generally if you want your data sorted in a groupby but it’s not one of the columns which are going to be grouped on then it’s better to sort the df prior to performing groupby:
In [5]:
df.sort_values('B').groupby('A').first()
Out[5]:
B
A
bar 1
foo 1