Fix javascript array sorting function