@Override
    protected S map(final Traverser.Admin<S> traverser) {
        final S start = traverser.get();

        // modulate each traverser object and keep its value to sort on as Pair<traverser-object,List<modulated-object>>
        // as of 3.6.0 this transformation occurs in place and values held in memory so that unproductive by() values
        // can be filtered. without this trade-off for more memory it would be necessary to apply the modulation twice
        // once for the filter and once for the compare.
        if (start instanceof Collection) {
            final Collection<S> original = (Collection<S>) start;
            final List<Pair<S, List<C>>> filteredAndModulated = filterAndModulate(original);
            return (S) filteredAndModulated.stream().map(Pair::getValue0).collect(Collectors.toList());
        } else if (start instanceof Map) {
            final List<Pair<S, List<C>>> filteredAndModulated = filterAndModulate(((Map) start).entrySet());
            final LinkedHashMap sortedMap = new LinkedHashMap();
            filteredAndModulated.stream().map(Pair::getValue0).map(entry -> (Map.Entry) entry).
                    forEach(entry -> sortedMap.put(entry.getKey(), entry.getValue()));
            return (S) sortedMap;
        } else if (start != null && start.getClass().isArray()) {
            final Collection<S> original = (Collection<S>) IteratorUtils.asList(start);
            final List<Pair<S, List<C>>> filteredAndModulated = filterAndModulate(original);
            return (S) filteredAndModulated.stream().map(Pair::getValue0).collect(Collectors.toList());
        }

        return start;
    }
@author Marko A. Rodriguez (http://markorodriguez.com)
gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/OrderLocalStep.java