Sorting collections
So far, we’ve learned how to create collections and how to perform basic operations on them. They have a lot of useful built-in methods, and one of them helps us sort collections. The reason we are paying attention to this one is because it’s not as straightforward as some of the others.
Some types have a natural order, such as numbers. They can be easily sorted from small to large. The same goes for strings – we can sort them A-Z. But how do we sort a collection that contains custom objects of the Task
type?
Stick with me – soon, you’ll be able to do both natural ordering and custom ordering while using the sort
method that’s built into collections.
Natural ordering
When we talk about natural ordering, we mean the default sorting order for a particular data type. For example, numbers are sorted in ascending order, while strings are sorted lexicographically. But still, Java wouldn’t know this without us...