Java Array Sort sortNullElements(Object[] array)

Here you can find the source of sortNullElements(Object[] array)

Description

Move all null elements to end of array.

License

Open Source License

Parameter

Parameter Description
array a parameter

Declaration

public static Object[] sortNullElements(Object[] array) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2004-2013//from   ww w . j  a  v a  2 s . c om
 * Contributors: L. Armanet, M. Camerlenghi, L. Cardonne, S. Delageniere,
 *               L. Duparchy, S. Ohlsson, P. Pascal, I. Schneider, S.Schulze,
 *               F. Torres
 * 
 * This file is part of the MIS tools package.
 * 
 * The MIS tools package is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * The MIS tools package 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 Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with the MIS tools package.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

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

import java.util.List;

public class Main {
    /**
     * Move all null elements to end of array. The order of the other elements remains. Afterwards a loop that is only
     * interested in all elements that are not null can break as soon as it finds a null element.
     * 
     * @param array
     * @return
     */
    public static Object[] sortNullElements(Object[] array) {
        if (array == null) {
            return null;
        }
        if (array.length == 0) {
            return array;
        }
        Object[] tmp = new Object[array.length];
        int k = 0;
        for (int i = 0; i < array.length; i++) {
            if (array[i] != null) {
                tmp[k++] = array[i];
            }
        }
        System.arraycopy(tmp, 0, array, 0, tmp.length);
        return array;
    }

    /**
     * Move all null elements to end of the list. The order of the other elements remains. Afterwards a loop that is
     * only interested in all elements that are not null can break as soon as it finds a null element.
     * 
     * @param col
     * @return
     */
    public static <T> List<T> sortNullElements(List<T> col) {
        if (col == null) {
            return null;
        }
        if (col.isEmpty()) {
            return col;
        }
        // Sorting
        Collections.sort(col, new Comparator<T>() {
            @Override
            public int compare(T obj1, T obj2) {
                if ((obj1 == null && obj2 == null)
                        || (obj1 != null && obj2 != null))
                    return 0;
                // null objects go to the end
                if (obj1 != null && obj2 == null)
                    return -1;
                return 1;
            }
        });
        return col;
    }
}

Related

  1. sortMinToMax(double[] v)
  2. sortNames(String[] names)
  3. sortNames(String[] names)
  4. sortNames(String[] names)
  5. sortNames(String[] names)
  6. sortPerm(final int[] w)
  7. sortPermutation(final int[] A)
  8. sortPixels(double[] data, int numPixel)
  9. sortPopup(Integer[] integers)