fix bug in environment sorting