public CloseableIterator<T> mergeSort(Iterator<T> values) throws IOException {
        ChunkSizeIterator<T> csi = new ChunkSizeIterator<T>(values, config.chunkSize);
        if (csi.isMultipleChunks()) {
            List<File> sortedChunks = writeSortedChunks(csi);
            return mergeSortedChunks(sortedChunks);
        } else {
            if (config.distinct) {
                SortedSet<T> list = new TreeSet<T>(comparator);
                while (csi.hasNext()) {
                    list.add(csi.next());
                }
                return new DelegatingMergeIterator<T>(list.iterator());
            } else {
                List<T> list = new ArrayList<T>(csi.getHeadSize());
                while (csi.hasNext()) {
                    list.add(csi.next());
                }
                Collections.sort(list, comparator);
                return new DelegatingMergeIterator<T>(list.iterator());
            }
        }
    }
public CloseableIterator < T > mergeSort ( Iterator < T > values ) throws IOException { ChunkSizeIterator < T > csi = new ChunkSizeIterator < T > ( values , config . chunkSize ) ; if ( csi . isMultipleChunks ( ) ) { List < File > sortedChunks = writeSortedChunks ( csi ) ; return mergeSortedChunks ( sortedChunks ) ; } else { if ( config . distinct ) { SortedSet < T > list = new TreeSet < T > ( comparator ) ; while ( csi . hasNext ( ) ) { list . add ( csi . next ( ) ) ; } return new DelegatingMergeIterator < T > ( list . iterator ( ) ) ; } else { List < T > list = new ArrayList < T > ( csi . getHeadSize ( ) ) ; while ( csi . hasNext ( ) ) { list . add ( csi . next ( ) ) ; } Collections . sort ( list , comparator ) ; return new DelegatingMergeIterator < T > ( list . iterator ( ) ) ; } } }
Performs an external merge on the values in the iterator.
@param values Iterator containing the data to sort.
@return an iterator the iterates over the sorted result.
@throws IOException if something fails when doing I/O.