environment sort logic