Source code
Description: source: package: org.apache.commons.lang3, class: ArraySorter

    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      https://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.lang3;
    018
    019import java.util.Arrays;
    020import java.util.Comparator;
    021
    022/**
    023 * Sorts and returns arrays in the fluent style.
    024 *
    025 * TODO For 4.0, rename to ArraySort, since we cover the sort() method here, see also ArrayFill.
    026 * @since 3.12.0
    027 */
    028public class ArraySorter {
    029
    030    /**
    031     * Sorts the given array into ascending order and returns it.
    032     *
    033     * @param array the array to sort (may be null).
    034     * @return the given array.
    035     * @see Arrays#sort(byte[])
    036     */
    037    public static byte[] sort(final byte[] array) {
    038        if (array != null) {
    039            Arrays.sort(array);
    040        }
    041        return array;
    042    }
    043
    044    /**
    045     * Sorts the given array into ascending order and returns it.
    046     *
    047     * @param array the array to sort (may be null).
    048     * @return the given array.
    049     * @see Arrays#sort(char[])
    050     */
    051    public static char[] sort(final char[] array) {
    052        if (array != null) {
    053            Arrays.sort(array);
    054        }
    055        return array;
    056    }
    057
    058    /**
    059     * Sorts the given array into ascending order and returns it.
    060     *
    061     * @param array the array to sort (may be null).
    062     * @return the given array.
    063     * @see Arrays#sort(double[])
    064     */
    065    public static double[] sort(final double[] array) {
    066        if (array != null) {
    067            Arrays.sort(array);
    068        }
    069        return array;
    070    }
    071
    072    /**
    073     * Sorts the given array into ascending order and returns it.
    074     *
    075     * @param array the array to sort (may be null).
    076     * @return the given array.
    077     * @see Arrays#sort(float[])
    078     */
    079    public static float[] sort(final float[] array) {
    080        if (array != null) {
    081            Arrays.sort(array);
    082        }
    083        return array;
    084    }
    085
    086    /**
    087     * Sorts the given array into ascending order and returns it.
    088     *
    089     * @param array the array to sort (may be null).
    090     * @return the given array.
    091     * @see Arrays#sort(int[])
    092     */
    093    public static int[] sort(final int[] array) {
    094        if (array != null) {
    095            Arrays.sort(array);
    096        }
    097        return array;
    098    }
    099
    100    /**
    101     * Sorts the given array into ascending order and returns it.
    102     *
    103     * @param array the array to sort (may be null).
    104     * @return the given array.
    105     * @see Arrays#sort(long[])
    106     */
    107    public static long[] sort(final long[] array) {
    108        if (array != null) {
    109            Arrays.sort(array);
    110        }
    111        return array;
    112    }
    113
    114    /**
    115     * Sorts the given array into ascending order and returns it.
    116     *
    117     * @param array the array to sort (may be null).
    118     * @return the given array.
    119     * @see Arrays#sort(short[])
    120     */
    121    public static short[] sort(final short[] array) {
    122        if (array != null) {
    123            Arrays.sort(array);
    124        }
    125        return array;
    126    }
    127
    128    /**
    129     * Sorts the given array into ascending order and returns it.
    130     *
    131     * @param <T> the array type.
    132     * @param array the array to sort (may be null).
    133     * @return the given array.
    134     * @see Arrays#sort(Object[])
    135     */
    136    public static <T> T[] sort(final T[] array) {
    137        if (array != null) {
    138            Arrays.sort(array);
    139        }
    140        return array;
    141    }
    142
    143    /**
    144     * Sorts the given array into ascending order and returns it.
    145     *
    146     * @param <T> the array type.
    147     * @param array the array to sort (may be null).
    148     * @param comparator the comparator to determine the order of the array. A {@code null} value uses the elements'
    149     *        {@link Comparable natural ordering}.
    150     * @return the given array.
    151     * @see Arrays#sort(Object[])
    152     */
    153    public static <T> T[] sort(final T[] array, final Comparator<? super T> comparator) {
    154        if (array != null) {
    155            Arrays.sort(array, comparator);
    156        }
    157        return array;
    158    }
    159
    160    /**
    161     * Constructs a new instance.
    162     *
    163     * @deprecated Will be removed in 4.0.0.
    164     */
    165    @Deprecated
    166    public ArraySorter() {
    167        // empty
    168    }
    169
    170}