![]() ![]() When you are working with data that has more complex attributes, you will want to write classes to represent these objects and how they will be compared to each other using their attributes. But oftentimes, your ArrayLists will be storing non-primitive data types. How to use Collections to sort non-primitive data types in JavaSo far, you’ve seen that sorting ArrayLists of strings or ints in Java using the Collections.sort() method is as easy as one line of code. Now, colors have been reverse sorted in-place so that if you printed it out, you would get the following output: ![]() It can be called like this:Ĭollections.sort(colors, Collections.reverseOrder()) If you are sorting an ArrayList of primitive data types, then Java Collections already provides you with a reverseOrder() comparator. Thankfully, Collections.sort()is overwritten with an optional 2nd parameter, which allows you to do just this:īut what’s a comparator? Well, a comparator is simply a function that compares two inputs and returns a number representing which input comes first. It would make much more sense to sort the list in descending order (highest scores first), so that the answers you are looking for are right at the top. But what if you wanted to sort in descending order? There are definitely cases where this makes sense-imagine that you had a list of test scores for a certain class, and you wanted to figure out who the top-scoring students are. How easy was that?! It would be just as easy to use Collections.sort() to sort into ascending order a list of Integers, Floats, or any other simple data type for that matter. If you were to print out the list, as such: Ta-da! Your colors list has now been sorted in-place. How might you do this? Using, sorting is as easy as a one-liner: You want to be able to print out the colors list, but in alphabetical order. Suppose you have the following simple program: ![]() ![]() The java.util package contains many useful utilities and packages which are often used by developers, including the ArrayList. How to Sort an ArrayList in Java using the Java Collections sort methodLet’s talk about Java Collections.sort method. In this article, we will go over how to sort any List implementation in Java (including ArrayList) using the Collections sort method. The more data you are working with, the more important it is that you are using it as effectively and efficiently as possible. On the other hand, if the roster was alphabetized, not only do you have to be less rigorous in your search, but you could easily jump to the section in the roster with the same first letter, and continue to jump around pages in big leaps until you land upon the person you were looking for. If the roster you were given was not in alphabetical order, you would need to flip through every single page and look very carefully to make sure you didn’t skip a single name anywhere, until you finally find the name you are looking for. There may be tens of thousands of students at this school. In today's lesson, we'll talk about Java Collections sort().Īs a quick thought experiment: imagine that you are searching for a name through a university’s roster. One of the most common functions we need as developers dealing with data is sorting-by being able to sort our data, we can optimize our algorithms to run much faster, and find the data it needs to in a fraction of the time that it would take to otherwise. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |