1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.lang3;
18
19 import java.util.Arrays;
20 import java.util.Comparator;
21
22 /**
23 * Sorts and returns arrays in the fluent style.
24 *
25 * TODO For 4.0, rename to ArraySort, since we cover the sort() method here, see also ArrayFill.
26 *
27 * @since 3.12.0
28 */
29 public class ArraySorter {
30
31 /**
32 * Sorts the given array into ascending order and returns it.
33 *
34 * @param array the array to sort (may be null).
35 * @return the given array.
36 * @see Arrays#sort(byte[])
37 */
38 public static byte[] sort(final byte[] array) {
39 if (array != null) {
40 Arrays.sort(array);
41 }
42 return array;
43 }
44
45 /**
46 * Sorts the given array into ascending order and returns it.
47 *
48 * @param array the array to sort (may be null).
49 * @return the given array.
50 * @see Arrays#sort(char[])
51 */
52 public static char[] sort(final char[] array) {
53 if (array != null) {
54 Arrays.sort(array);
55 }
56 return array;
57 }
58
59 /**
60 * Sorts the given array into ascending order and returns it.
61 *
62 * @param array the array to sort (may be null).
63 * @return the given array.
64 * @see Arrays#sort(double[])
65 */
66 public static double[] sort(final double[] array) {
67 if (array != null) {
68 Arrays.sort(array);
69 }
70 return array;
71 }
72
73 /**
74 * Sorts the given array into ascending order and returns it.
75 *
76 * @param array the array to sort (may be null).
77 * @return the given array.
78 * @see Arrays#sort(float[])
79 */
80 public static float[] sort(final float[] array) {
81 if (array != null) {
82 Arrays.sort(array);
83 }
84 return array;
85 }
86
87 /**
88 * Sorts the given array into ascending order and returns it.
89 *
90 * @param array the array to sort (may be null).
91 * @return the given array.
92 * @see Arrays#sort(int[])
93 */
94 public static int[] sort(final int[] array) {
95 if (array != null) {
96 Arrays.sort(array);
97 }
98 return array;
99 }
100
101 /**
102 * Sorts the given array into ascending order and returns it.
103 *
104 * @param array the array to sort (may be null).
105 * @return the given array.
106 * @see Arrays#sort(long[])
107 */
108 public static long[] sort(final long[] array) {
109 if (array != null) {
110 Arrays.sort(array);
111 }
112 return array;
113 }
114
115 /**
116 * Sorts the given array into ascending order and returns it.
117 *
118 * @param array the array to sort (may be null).
119 * @return the given array.
120 * @see Arrays#sort(short[])
121 */
122 public static short[] sort(final short[] array) {
123 if (array != null) {
124 Arrays.sort(array);
125 }
126 return array;
127 }
128
129 /**
130 * Sorts the given array into ascending order and returns it.
131 *
132 * @param <T> the array type.
133 * @param array the array to sort (may be null).
134 * @return the given array.
135 * @see Arrays#sort(Object[])
136 */
137 public static <T> T[] sort(final T[] array) {
138 if (array != null) {
139 Arrays.sort(array);
140 }
141 return array;
142 }
143
144 /**
145 * Sorts the given array into ascending order and returns it.
146 *
147 * @param <T> the array type.
148 * @param array the array to sort (may be null).
149 * @param comparator the comparator to determine the order of the array. A {@code null} value uses the elements'
150 * {@link Comparable natural ordering}.
151 * @return the given array.
152 * @see Arrays#sort(Object[])
153 */
154 public static <T> T[] sort(final T[] array, final Comparator<? super T> comparator) {
155 if (array != null) {
156 Arrays.sort(array, comparator);
157 }
158 return array;
159 }
160
161 /**
162 * Constructs a new instance.
163 *
164 * @deprecated Will be removed in 4.0.0.
165 */
166 @Deprecated
167 public ArraySorter() {
168 // empty
169 }
170
171 }