    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 *      http://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 and returns the given array.
    032     *
    033     * @param array the array to sort.
    034     * @return the given array.
    035     * @see Arrays#sort(byte[])
    036     */
    037    public static byte[] sort(final byte[] array) {
    038        Arrays.sort(array);
    039        return array;
    040    }
    041
    042    /**
    043     * Sorts and returns the given array.
    044     *
    045     * @param array the array to sort.
    046     * @return the given array.
    047     * @see Arrays#sort(char[])
    048     */
    049    public static char[] sort(final char[] array) {
    050        Arrays.sort(array);
    051        return array;
    052    }
    053
    054    /**
    055     * Sorts and returns the given array.
    056     *
    057     * @param array the array to sort.
    058     * @return the given array.
    059     * @see Arrays#sort(double[])
    060     */
    061    public static double[] sort(final double[] array) {
    062        Arrays.sort(array);
    063        return array;
    064    }
    065
    066    /**
    067     * Sorts and returns the given array.
    068     *
    069     * @param array the array to sort.
    070     * @return the given array.
    071     * @see Arrays#sort(float[])
    072     */
    073    public static float[] sort(final float[] array) {
    074        Arrays.sort(array);
    075        return array;
    076    }
    077
    078    /**
    079     * Sorts and returns the given array.
    080     *
    081     * @param array the array to sort.
    082     * @return the given array.
    083     * @see Arrays#sort(int[])
    084     */
    085    public static int[] sort(final int[] array) {
    086        Arrays.sort(array);
    087        return array;
    088    }
    089
    090    /**
    091     * Sorts and returns the given array.
    092     *
    093     * @param array the array to sort.
    094     * @return the given array.
    095     * @see Arrays#sort(long[])
    096     */
    097    public static long[] sort(final long[] array) {
    098        Arrays.sort(array);
    099        return array;
    100    }
    101
    102    /**
    103     * Sorts and returns the given array.
    104     *
    105     * @param array the array to sort.
    106     * @return the given array.
    107     * @see Arrays#sort(short[])
    108     */
    109    public static short[] sort(final short[] array) {
    110        Arrays.sort(array);
    111        return array;
    112    }
    113
    114    /**
    115     * Sorts and returns the given array.
    116     *
    117     * @param <T> the array type.
    118     * @param array the array to sort.
    119     * @return the given array.
    120     * @see Arrays#sort(Object[])
    121     */
    122    public static <T> T[] sort(final T[] array) {
    123        Arrays.sort(array);
    124        return array;
    125    }
    126
    127    /**
    128     * Sorts and returns the given array.
    129     *
    130     * @param <T> the array type.
    131     * @param array the array to sort.
    132     * @param comparator the comparator to determine the order of the array. A {@code null} value uses the elements'
    133     *        {@link Comparable natural ordering}.
    134     * @return the given array.
    135     * @see Arrays#sort(Object[])
    136     */
    137    public static <T> T[] sort(final T[] array, final Comparator<? super T> comparator) {
    138        Arrays.sort(array, comparator);
    139        return array;
    140    }
    141
    142}