Java Collection Sort toSortedStrings(final Collection objects)

Here you can find the source of toSortedStrings(final Collection objects)

Description

Returns a sorted list of the strings corresponding to the given collection of objects.

License

Open Source License

Parameter

Parameter Description
objects the given collection of objects

Return

a sorted list of the strings

Declaration

public static List<String> toSortedStrings(final Collection<? extends Object> objects) 

Method Source Code

//package com.java2s;
/*//from   www .  java  2 s. c o m
 * StringUtils.java
 *
 * Created on October 4, 2006, 2:36 PM
 *
 * Description:
 *
 * Copyright (C) 2006 Stephen L. Reed.
 *
 * This program is free software; you can redistribute it and/or modify it under the terms
 * of the GNU General Public License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program;
 * if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class Main {
    /** Returns a sorted list of the strings corresponding to the given collection of objects.
     *
     * @param objects the given collection of objects
     * @return a sorted list of the strings
     */
    public static List<String> toSortedStrings(final Collection<? extends Object> objects) {
        //Preconditions
        assert objects != null : "objects must not be null";

        final List<String> strings = new ArrayList<>();
        for (final Object obj : objects) {
            strings.add(obj.toString());
        }
        Collections.sort(strings);
        return strings;
    }
}

Related

  1. sortedByName(Collection unsorted, Comparator comparator, T... exclusions)
  2. sortedCollection(final Comparator comparator, final Collection input)
  3. sortedIfPossible(Collection collection)
  4. sortEntities(Collection entities, Comparator comparator)
  5. sortMapByValues( final Map> related_region_counts, Comparator>> comparator)