with sorting algorithm