Java Collection Null sortNumbers( Collection values, boolean removeNull)

Here you can find the source of sortNumbers( Collection values, boolean removeNull)

Description

Utility method which takes a Collection of Numbers, and returns this back as a List, in order of each Number's double value If removeNull is true, null values will be ignored and not returned If removeNull is false, null values will be sorted to the end of the list

License

Open Source License

Parameter

Parameter Description
values The Collection of Numbers to sort

Return

A List of the Numbers, sorted in ascending order by double value

Declaration

public static <T extends Number> List<T> sortNumbers(
        Collection<T> values, boolean removeNull) 

Method Source Code

//package com.java2s;
/**//from   w w w. j av a  2s.c  om
 * The contents of this file are subject to the OpenMRS Public License
 * Version 1.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://license.openmrs.org
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * Copyright (C) OpenMRS, LLC.  All Rights Reserved.
 */

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

public class Main {
    /**
     * Utility method which takes a Collection of Numbers, and returns
     * this back as a List, in order of each Number's double value
     * If removeNull is true, null values will be ignored and not returned
     * If removeNull is false, null values will be sorted to the end of the list
     * @param values The Collection of Numbers to sort
     * @return A List of the Numbers, sorted in ascending order by double value
     */
    public static <T extends Number> List<T> sortNumbers(
            Collection<T> values, boolean removeNull) {
        List<T> valueList = new ArrayList<T>(values);
        if (removeNull) {
            values.remove(null);
        }
        Collections.sort(valueList, new Comparator<T>() {
            public int compare(T n1, T n2) {
                if (n1 == null) {
                    return n2 == null ? 0 : 1;
                } else if (n2 == null) {
                    return -1;
                }
                Double d1 = Double.valueOf(n1.doubleValue());
                Double d2 = Double.valueOf(n2.doubleValue());
                return d1.compareTo(d2);
            }
        });
        return valueList;
    }
}

Related

  1. removeNull(final Collection c)
  2. removeNullElement(Collection collection)
  3. removeNulls(Collection p_collection)
  4. removeNulls(F collection)
  5. removeNulls(final Collection collection)
  6. stripNulls(T collection)
  7. trimToNull(T collection)