variable reference problem, try to fix temporary sorting method