move sorting logic to javascript