Sorting Arrays
import java.util.Arrays;
You code should look like ours below:
Arrays.sort( aryNums );
First you type the word "Arrays", then a dot. As soon as you type a dot, NetBeans will display a list of things you can do with arrays. Type the word "sort". In between a pair of round brackets, you then put the name of the array you want to sort. (Notice that you don't need any square brackets after the array name.)And that's it - that's enough to sort the array! Here's some code to try out:
Sorting in descending order, however, is only possible either by writing your own sorting code, or converting your array to Integer objects then importing from the Collections library. If you need to a descending sort, here's some code that does just that (skip this code, if you want):
In the next lesson, we'll take a look at arrays and strings.
Comments
Post a Comment