added optional sorting argument