What is an Array?

  • Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.
  • To declare an array, define the variable type with square brackets:
[pastacode lang=”java” manual=”String%5B%5D%20subject%3B” message=”” highlight=”” provider=”manual”/]
  • We have now declared a variable that holds an array of strings. To insert values to it, we can use an array literal – place the values in a comma-separated list, inside curly braces:
[pastacode lang=”java” manual=”String%5B%5D%20subject%20%3D%20%7B%22HTML%22%2C%20%22CSS%22%2C%20%22PHP%22%2C%20%22PYTHON%22%2C%20%22JAVA%22%7D%3B” message=”” highlight=”” provider=”manual”/]

Arrays.sort() in java

  • The sorting may be a way to arrange elements of a list or array during a certain order.
  • The order could also be in ascending or descending order.
  • The numerical and lexicographical (alphabetical) order may be a widely used order.
  • sort() method is a java.util.Arrays class method.

Syntax

[pastacode lang=”java” manual=”public%20static%20void%20sort(int%5B%5D%20arr%2C%20int%20from_Index%2C%20int%20to_Index)” message=”” highlight=”” provider=”manual”/]
  • arr – The array to be sorted.
  • from_Index – The indexof the first element, inclusive, to be sorted.
  • to_Index – The index of the last element, exclusive, to be sorted.

This method doesn’t return any value.

Sort an array of integers in ascending order.

  • The ascending order arranges the elements within the lowest to highest order. It’s also referred to as natural order or numerical order
[pastacode lang=”java” manual=”%2F%2F%20A%20sample%20Java%20program%20to%20sort%20an%20array%20of%20integers%0A%2F%2F%20using%20Arrays.sort().%20It%20by%20default%20sorts%20in%0A%2F%2F%20ascending%20order%0Aimport%20java.util.Arrays%3B%0A%20%0Apublic%20class%20SortExample%0A%7B%0A%20%20%20%20public%20static%20void%20main(String%5B%5D%20args)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20Our%20arr%20contains%208%20elements%0A%20%20%20%20%20%20%20%20int%5B%5D%20arr%20%3D%20%7B8%2C%204%2C%2070%2C%202%2C%2018%2C%2026%2C%20100%2C%2085%7D%3B%0A%20%0A%20%20%20%20%20%20%20%20Arrays.sort(arr)%3B%0A%20%0A%20%20%20%20%20%20%20%20System.out.printf(%22Modified%20arr%5B%5D%20%3A%20%25s%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Arrays.toString(arr))%3B%0A%20%20%20%20%7D%0A%7D%0A” message=”” highlight=”” provider=”manual”/]

Output

[pastacode lang=”bash” manual=”Modified%20arr%5B%5D%20%3A%20%5B2%2C%204%2C%208%2C%2018%2C%2026%2C%2070%2C%2085%2C%20100%5D” message=”” highlight=”” provider=”manual”/]

Use sort() to sort a subarray of arr[]

  • An array derived from the array is understood as subarray. Suppose, a[] is an array having the elements [12, 90, 34, 2, 45, 3, 22, 18, 5, 78] and that we want to sort array elements from 34 to 18. It’ll sort the subarray [34, 2, 45, 3, 22, 18] and keep the other elements because it is.
  • To sort the subarray, the Arrays class provides the static method named sort(). It sorts the required range of the array into ascending order. We will also sort the array of type long, double, float, char, byte, etc.

Syntax

[pastacode lang=”java” manual=”public%20static%20void%20sort(int%5B%5D%20a%2C%20int%20fromIndex%2C%20int%20toIndex)%20″ message=”” highlight=”” provider=”manual”/]

The method parses the following three parameters:

  • a: An array to be sort.
  • fromIndex: The index of the primary element of the subarray. It participates within the sorting.
  • toIndex: The index of the last element of the subarray. It doesn’t participate within the sorting.
[pastacode lang=”java” manual=”%2F%2F%20A%20sample%20Java%20program%20to%20sort%20a%20subarray%0A%2F%2F%20using%20Arrays.sort().%0Aimport%20java.util.Arrays%3B%0A%20%0Apublic%20class%20SortExample%0A%7B%0A%20%20%20%20public%20static%20void%20main(String%5B%5D%20args)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20Our%20arr%20contains%208%20elements%0A%20%20%20%20%20%20%20%20int%5B%5D%20arr%20%3D%20%7B13%2C%207%2C%206%2C%2045%2C%2021%2C%209%2C%202%2C%20100%7D%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Sort%20subarray%20from%20index%201%20to%204%2C%20i.e.%2C%0A%20%20%20%20%20%20%20%20%2F%2F%20only%20sort%20subarray%20%7B7%2C%206%2C%2045%2C%2021%7D%20and%0A%20%20%20%20%20%20%20%20%2F%2F%20keep%20other%20elements%20as%20it%20is.%0A%20%20%20%20%20%20%20%20Arrays.sort(arr%2C%201%2C%205)%3B%0A%20%0A%20%20%20%20%20%20%20%20System.out.printf(%22Modified%20arr%5B%5D%20%3A%20%25s%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Arrays.toString(arr))%3B%0A%20%20%20%20%7D%0A%7D%0A” message=”” highlight=”” provider=”manual”/]

Output

[pastacode lang=”bash” manual=”Modified%20arr%5B%5D%20%3A%20%5B13%2C%206%2C%207%2C%2021%2C%2045%2C%209%2C%202%2C%20100%5D” message=”” highlight=”” provider=”manual”/]

Sort Array in Descending Order

  • The descending order arranges the elements within the highest to lowest order.
[pastacode lang=”java” manual=”%2F%2F%20A%20sample%20Java%20program%20to%20sort%20a%20subarray%0A%2F%2F%20in%20descending%20order%20using%20Arrays.sort().%0Aimport%20java.util.Arrays%3B%0Aimport%20java.util.Collections%3B%0A%20%0Apublic%20class%20SortExample%0A%7B%0A%20%20%20%20public%20static%20void%20main(String%5B%5D%20args)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20Note%20that%20we%20have%20Integer%20here%20instead%20of%0A%20%20%20%20%20%20%20%20%2F%2F%20int%5B%5D%20as%20Collections.reverseOrder%20doesn’t%0A%20%20%20%20%20%20%20%20%2F%2F%20work%20for%20primitive%20types.%0A%20%20%20%20%20%20%20%20Integer%5B%5D%20arr%20%3D%20%7B8%2C%204%2C%2070%2C%202%2C%2018%2C%2026%2C%20100%2C%2085%7D%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Sorts%20arr%5B%5D%20in%20descending%20order%0A%20%20%20%20%20%20%20%20Arrays.sort(arr%2C%20Collections.reverseOrder())%3B%0A%20%0A%20%20%20%20%20%20%20%20System.out.printf(%22Modified%20arr%5B%5D%20%3A%20%25s%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Arrays.toString(arr))%3B%0A%20%20%20%20%7D%0A%7D%0A” message=”” highlight=”” provider=”manual”/]

Output

[pastacode lang=”bash” manual=”Modified%20arr%5B%5D%20%3A%20%5B100%2C%2085%2C%2070%2C%2026%2C%2018%2C%208%2C%204%2C%202%5D” message=”” highlight=”” provider=”manual”/]

We can also sort strings in alphabetical order

[pastacode lang=”java” manual=”%2F%2F%20A%20sample%20Java%20program%20to%20sort%20an%20array%20of%20strings%0A%2F%2F%20in%20ascending%20and%20descending%20orders%20using%20Arrays.sort().%0Aimport%20java.util.Arrays%3B%0Aimport%20java.util.Collections%3B%0A%20%0Apublic%20class%20SortExample%0A%7B%0A%20%20%20%20public%20static%20void%20main(String%5B%5D%20args)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20String%20arr%5B%5D%20%3D%20%7B%22practice.wikitechy.org%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22quiz.wikitechy.org%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22code.wikitechy.org%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Sorts%20arr%5B%5D%20in%20ascending%20order%0A%20%20%20%20%20%20%20%20Arrays.sort(arr)%3B%0A%20%20%20%20%20%20%20%20System.out.printf(%22Modified%20arr%5B%5D%20%3A%20%5Cn%25s%5Cn%5Cn%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Arrays.toString(arr))%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Sorts%20arr%5B%5D%20in%20descending%20order%0A%20%20%20%20%20%20%20%20Arrays.sort(arr%2C%20Collections.reverseOrder())%3B%0A%20%0A%20%20%20%20%20%20%20%20System.out.printf(%22Modified%20arr%5B%5D%20%3A%20%5Cn%25s%5Cn%5Cn%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Arrays.toString(arr))%3B%0A%20%20%20%20%7D%0A%7D%0A” message=”” highlight=”” provider=”manual”/]

Output

[pastacode lang=”bash” manual=”Modified%20arr%5B%5D%20%3A%20%0AModified%20arr%5B%5D%20%3A%20%0A%5Bquiz.wikitechy.org%2C%20practice.wikitechy.org%2C%20code.wikitechy.org%5D%0A” message=”” highlight=”” provider=”manual”/]

We can also sort an array according to user defined criteria

  • We use Comparator interface for this purpose.
[pastacode lang=”java” manual=”%2F%2F%20Java%20program%20to%20demonstrate%20working%20of%20Comparator%0A%2F%2F%20interface%0Aimport%20java.util.*%3B%0Aimport%20java.lang.*%3B%0Aimport%20java.io.*%3B%0A%20%0A%2F%2F%20A%20class%20to%20represent%20a%20student.%0Aclass%20Student%0A%7B%0A%20%20%20%20int%20rollno%3B%0A%20%20%20%20String%20name%2C%20subject%3B%0A%20%0A%20%20%20%20%2F%2F%20Constructor%0A%20%20%20%20public%20Student(int%20rollno%2C%20String%20name%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20String%20subject)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20this.rollno%20%3D%20rollno%3B%0A%20%20%20%20%20%20%20%20this.name%20%3D%20name%3B%0A%20%20%20%20%20%20%20%20this.subject%20%3D%20subject%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Used%20to%20print%20student%20details%20in%20main()%0A%20%20%20%20public%20String%20toString()%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20return%20this.rollno%20%2B%20%22%20%22%20%2B%20this.name%20%2B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%20%22%20%2B%20this.subject%3B%0A%20%20%20%20%7D%0A%7D%0A%20%0Aclass%20Sortbyroll%20implements%20Comparator%3CStudent%3E%0A%7B%0A%20%20%20%20%2F%2F%20Used%20for%20sorting%20in%20ascending%20order%20of%0A%20%20%20%20%2F%2F%20roll%20number%0A%20%20%20%20public%20int%20compare(Student%20a%2C%20Student%20b)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20return%20a.rollno%20-%20b.rollno%3B%0A%20%20%20%20%7D%0A%7D%0A%20%0A%2F%2F%20Driver%20class%0Aclass%20Main%0A%7B%0A%20%20%20%20public%20static%20void%20main%20(String%5B%5D%20args)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20Student%20%5B%5D%20arr%20%3D%20%7Bnew%20Student(111%2C%20%22bbbb%22%2C%20%22html%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20new%20Student(131%2C%20%22aaaa%22%2C%20%22css%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20new%20Student(121%2C%20%22cccc%22%2C%20%22php%22)%7D%3B%0A%20%0A%20%20%20%20%20%20%20%20System.out.println(%22Unsorted%22)%3B%0A%20%20%20%20%20%20%20%20for%20(int%20i%3D0%3B%20i%3Carr.length%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.println(arr%5Bi%5D)%3B%0A%20%0A%20%20%20%20%20%20%20%20Arrays.sort(arr%2C%20new%20Sortbyroll())%3B%0A%20%0A%20%20%20%20%20%20%20%20System.out.println(%22%5CnSorted%20by%20rollno%22)%3B%0A%20%20%20%20%20%20%20%20for%20(int%20i%3D0%3B%20i%3Carr.length%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.println(arr%5Bi%5D)%3B%0A%20%20%20%20%7D%0A%7D%0A%0A” message=”” highlight=”” provider=”manual”/]

Output

[pastacode lang=”bash” manual=”Unsorted%0A111%20bbbb%20html%0A131%20aaaa%20css%0A121%20cccc%20php%0A%0ASorted%20by%20rollno%0A111%20bbbb%20html%0A121%20cccc%20php%0A131%20aaaa%20css%0A” message=”” highlight=”” provider=”manual”/]

Categorized in: