Here you can find the source of sort(T[] array, boolean nullsFirst)
Parameter | Description |
---|---|
T | The type of objects to be sorted. All objects must be mutually comparable. |
array | the array to be sorted. |
nullsFirst | if true , all null elements will be grouped at the beginning of the array. Otherwise, they will be grouped at the end. |
public static <T> T[] sort(T[] array, boolean nullsFirst)
//package com.java2s; /*// w w w .j a v a 2 s .c o m * ArrayUtilities.java (Class: com.madphysicist.tools.util.ArrayUtilities) * * Mad Physicist JTools Project (General Purpose Utilities) * * The MIT License (MIT) * * Copyright (c) 2013 by Joseph Fox-Rabinovitz * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ import java.util.Arrays; public class Main { /** * Sorts an array with {@code null} elements according to the natural order * of the elements. {@code null} elements can be placed at the beginning or * the end of the sorted array. * * @param <T> The type of objects to be sorted. All objects must be mutually * comparable. * @param array the array to be sorted. * @param nullsFirst if {@code true}, all {@code null} elements will be * grouped at the beginning of the array. Otherwise, they will be grouped at * the end. * @return the sorted array, or {@code null} if {@code array} is {@code * null}. * @since 1.0.0 * @see java.util.Arrays#sort(Object[]) Arrays.sort() */ public static <T> T[] sort(T[] array, boolean nullsFirst) { if (array == null) return null; int nullPos; if (nullsFirst) { nullPos = 0; // swap nulls to the beginning of the array for (int i = 0; i < array.length; i++) { if (array[i] == null) { // swap with first non-null element if (i != nullPos) { array[i] = array[nullPos]; array[nullPos] = null; } nullPos++; } } Arrays.sort(array, nullPos, array.length); } else { nullPos = array.length; // swap nulls to the end of the array for (int i = array.length - 1; i >= 0; i--) { if (array[i] == null) { nullPos--; // swap with last non-null element if (i != nullPos) { array[i] = array[nullPos]; array[nullPos] = null; } } } Arrays.sort(array, 0, nullPos); } return array; } }