public UnsafeSorterIterator getSortedIterator() throws IOException {
        assert (recordComparator != null);
        if (spillWriters.isEmpty()) {
            assert (inMemSorter != null);
            readingIterator = new SpillableIterator(inMemSorter.getSortedIterator());
            return readingIterator;
        } else {
            final UnsafeSorterSpillMerger spillMerger =
                    new UnsafeSorterSpillMerger(recordComparator, prefixComparator, spillWriters.size());
            for (UnsafeSorterSpillWriter spillWriter : spillWriters) {
                spillMerger.addSpillIfNotEmpty(spillWriter.getReader());
            }
            if (inMemSorter != null) {
                readingIterator = new SpillableIterator(inMemSorter.getSortedIterator());
                spillMerger.addSpillIfNotEmpty(readingIterator);
            }
            return spillMerger.getSortedIterator();
        }
    }
Returns a sorted iterator. It is the caller's responsibility to call `cleanupResources()`
after consuming this iterator.
indexr-query-opt/src/main/java/org/apache/spark/util/collection/unsafe/sort/UnsafeExternalSorter.java