Java Collection Sort sorted(Collection collection)

Here you can find the source of sorted(Collection collection)

Description

Similar to #sorted(Collection,Comparator) using the Comparable interface of the given elements

License

Apache License

Parameter

Parameter Description
collection a parameter

Return

new sorted instance, null if given is null

Declaration

public static <E extends Comparable<E>> List<E> sorted(Collection<E> collection) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2011 Danny Kunz/*w ww .  j a  v a2  s. c o m*/
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

import java.util.ArrayList;

import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;

import java.util.List;

public class Main {
    /**
     * Similar to {@link #sorted(Collection, Comparator)} using the {@link Comparable} interface of the given elements
     * 
     * @param collection
     * @return new sorted {@link List} instance, null if given {@link Collection} is null
     */
    public static <E extends Comparable<E>> List<E> sorted(Collection<E> collection) {
        //
        final Comparator<E> comparator = null;
        return sorted(collection, comparator);
    }

    /**
     * Returns a new {@link List} instance which is based on the elements of the given {@link Collection} and which is sorted using
     * the given {@link Comparator}<br>
     * If the given {@link Comparator} is null the natural order is used. <br>
     * The given {@link Collection} will kept unmodified, only the returned {@link List} will be sorted.
     * 
     * @see Collections#sort(List, Comparator)
     * @param collection
     * @param comparator
     * @return a new {@link List} instance
     */
    public static <E> List<E> sorted(Collection<E> collection, Comparator<E> comparator) {
        //
        final List<E> retlist = collection != null ? new ArrayList<E>(collection) : null;

        //
        if (retlist != null) {
            Collections.sort(retlist, comparator);
        }

        //
        return retlist;
    }
}

Related

  1. sortByDescendingOrder(Map collection)
  2. sortClassesByLevelOfInheritance(Collection> classes)
  3. sorted(Collection coll)
  4. sorted(Collection input)
  5. sorted(Collection coll)
  6. sorted(Collection ss)
  7. sorted(Collection c)
  8. sorted(Collection c)
  9. sorted(Collection collection)