Note also, that the SortedDependingList does currently not allow to add an element from listA a second time - in this respect it actually works like a set of elements from listA because this is usually what you want in such a setting. We can use Collections.reverseOrder () method, which returns a Comparator, for reverse sorting. DigitalOcean makes it simple to launch in the cloud and scale up as you grow whether youre running one virtual machine or ten thousand. No spam ever. When we try to use sort over a zip object. Does Counterspell prevent from any further spells being cast on a given turn? That's right but the solutions use completely different methods which could be used for different applications. Wed like to help. Note: the key=operator.itemgetter(1) solves the duplicate issue, zip is not subscriptable you must actually use, If there is more than one matching it gets the first, This does not solve the OPs question. Stream.sorted() by default sorts in natural order. I like having a list of sorted indices. Linear Algebra - Linear transformation question, Acidity of alcohols and basicity of amines, Is there a solution to add special characters from software and how to do it. Key Selector Variant. Copyright 2011-2021 www.javatpoint.com. As you can see that we are using Collections.sort() method to sort the list of Strings. Getting key with maximum value in dictionary? All of the values at the end of the list will be in their order dictated by the list2. For cases like these, we'll want to write a custom Comparator: And now, when we execute this code, we've got the natural order of names, as well as ages, sorted: Here, we've used a Lambda expression to create a new Comparator implicitly and defined the logic for sorting/comparison. In this quick tutorial, we'll learn how to find items from one list based on values from another list using Java 8 Streams. The answer of riza might be useful when plotting data, since zip(*sorted(zip(X, Y), key=lambda pair: pair[0])) returns both the sorted X and Y sorted with values of X. Now it produces an iterable object. If we talk about the working of this method, then the method works on ASCII values. This could be done by wrapping listA inside a custom sorted list like so: Then you can use this custom list as follows: Of course, this custom list will only be valid as long as the elements in the original list do not change. How do I read / convert an InputStream into a String in Java? Sorting a 10000 items list 100 times improves speed 140 times (265 ms for the whole batch instead of 37 seconds) on my unit tests. This class has two parameters, firstName and lastName. Linear regulator thermal information missing in datasheet, How to tell which packages are held back due to phased updates. It's a List