**Arrays sort in Java:**

Arrays.sort() method in java is used to sort the elements in ascending or descending order.

Sorts the specified array of objects into ascending order, according to the natural ordering of its elements. All elements in the array must implement the `Comparable`

interface. Furthermore, all elements in the array must be *mutually comparable* (that is, `e1.compareTo(e2)`

must not throw a `ClassCastException`

for any elements `e1`

and `e2`

in the array).This sort is guaranteed to be *stable*: equal elements will not be reordered as a result of the sort.

**Implementation note:** This implementation is a stable, adaptive, iterative mergesort that requires far fewer than n lg(n) comparisons when the input array is partially sorted, while offering the performance of a traditional mergesort when the input array is randomly ordered. If the input array is nearly sorted, the implementation requires approximately n comparisons. Temporary storage requirements vary from a small constant for nearly sorted input arrays to n/2 object references for randomly ordered input arrays.

The implementation takes equal advantage of ascending and descending order in its input array, and can take advantage of ascending and descending order in different parts of the the same input array. It is well-suited to merging two or more sorted arrays: simply concatenate the arrays and sort the resulting array.

The implementation was adapted from Tim Peters’s list sort for Python ( TimSort). It uses techniques from Peter McIlroy’s “Optimistic Sorting and Information Theoretic Complexity”, in Proceedings of the Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474, January 1993.

**Parameters:**`a`

– the array to be sorted**Throws:**`ClassCastException`

– if the array contains elements that are not*mutually comparable*(for example, strings and integers)`IllegalArgumentException`

– (optional) if the natural ordering of the array elements is found to violate the`Comparable`

contract

**Example 1:**

1 2 3 |
String[] str = {"b", "c", "a", "d"}; Arrays.sort(str); System.out.println( Arrays.toString(str) ); |

1 2 3 |
Output: [a, b, c, d] |

**Example 2:**

1 2 3 |
int[] integers = {10, 20, 50, 40, 30}; Arrays.sort(integers); System.out.println(Arrays.toString(integers)); |

1 2 3 |
Output: [10, 20, 30, 40, 50] |

**Sort in descending order:**

1 2 3 4 5 6 |
import java.util.Arrays; import java.util.Collections; Integer[] integers = {10, 20, 50, 40, 30}; Arrays.sort(integers, Collections.reverseOrder()); System.out.println( Arrays.toString(integers) ); |

1 2 3 |
Output: [50, 40, 30, 20, 10] |

**Sort strings in alphabetical order:**

1 2 3 4 5 6 7 8 |
String[] str = {"aa", "ba", "cc"}; //sorts in ascending order Arrays.sort(str); System.out.println(Arrays.toString(str)); //sorts in descending order Arrays.sort(str, Collections.reverseOrder()); System.out.println(Arrays.toString(str)); |

1 2 3 4 |
Output: [aa, ba, cc] [cc, ba, aa] |

**Recommended Posts:**

Find the third largest number in an array in Java

Tags: arrays sort method in java