tag:blogger.com,1999:blog-8712770457197348465.post4663557490601728786..comments2024-03-19T01:04:48.783-07:00Comments on Javarevisited: How to sort ArrayList in Natural and Custom Order in Java - Example Tutorialjavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger17125tag:blogger.com,1999:blog-8712770457197348465.post-9293006411039738232024-03-04T23:01:10.947-08:002024-03-04T23:01:10.947-08:00import java.util.ArrayList;
import java.util.Colle...import java.util.ArrayList;<br />import java.util.Collections;<br />import java.util.Comparator;<br />import java.util.List;<br />import java.util.stream.Collectors;<br /><br />public class ArrayListSortingExample {<br /> private static class SmartPhone implements Comparable{<br /> private String brandName;<br /> private String productName;<br /> private int price;<br /><Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-74944776103719643212018-06-03T08:51:26.285-07:002018-06-03T08:51:26.285-07:00Some Java8 sorting examples:
Collections.sort(smar...Some Java8 sorting examples:<br />Collections.sort(smartPhones,Collections.reverseOrder((SmartPhone p1,SmartPhone p2)->p1.getPrice()-p2.getPrice()));<br /> Collections.sort(smartPhones,Comparator.comparing(SmartPhone::getPrice).reversed());Viraj Bhoslenoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-59347260865874039912014-11-29T07:26:41.720-08:002014-11-29T07:26:41.720-08:00Thanks for this simple example. I was looking to s...Thanks for this simple example. I was looking to sort an ArrayList of String alphabetically when I come through this tutorial, It helps me a lot. Thanks a ton.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-68075175491508325382013-09-14T13:44:01.394-07:002013-09-14T13:44:01.394-07:00Should be as follows:
public class ArrayListSorti...Should be as follows:<br /><br />public class ArrayListSortingExample {<br /><br /> private static class SmartPhone implements Comparable {<br /><br /> private String brand;<br /> private String model;<br /> private int price;<br /><br /> public SmartPhone(String brand, String model, int price) {<br /> this.brand = brand;<br /> this.model = Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-33293371630282209612013-08-10T13:48:40.592-07:002013-08-10T13:48:40.592-07:00My compiler complained that the interfaces were no...My compiler complained that the interfaces were not implemented. When I let it suggest corrections, it added compare(Object) and compareTo(Object,Object) methods. Further research revealed that both interfaces should have been 'parametized'. Basically, that means just adding to the Class definitions so that the first reads "private static class SmartPhone implements Comparable"Anonymoushttps://www.blogger.com/profile/14396440572260141251noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-45779686761155926372013-08-09T04:49:14.613-07:002013-08-09T04:49:14.613-07:00thank you very much for your kind information...thank you very much for your kind information...Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-80482157077156147972013-06-05T05:05:40.994-07:002013-06-05T05:05:40.994-07:00@Anonymous, What is the error you are getting? Can...@Anonymous, What is the error you are getting? Can you post compilation error here?Javin @ puzzle asked in programming interviewshttp://javarevisited.blogspot.com/2013/04/top-10-puzzles-riddles-logical-programming-question-lateral-thinking-interview.htmlnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-68133370362771480282013-06-05T05:02:40.821-07:002013-06-05T05:02:40.821-07:00Above code is not compilling in Java7.
Did anyone ...Above code is not compilling in Java7.<br />Did anyone noticed?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-13831149788290340002013-02-27T19:59:08.382-08:002013-02-27T19:59:08.382-08:00Hi,
i am not satisfied with the program.my query ...Hi,<br /><br />i am not satisfied with the program.my query is as :<br />we need to sort data dynamically according to date wise or int type or some time ascending or descending order.. please assist here...<br /><br /><br />Thx,<br />SaurabhAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-31491962650549952852012-12-09T20:52:09.951-08:002012-12-09T20:52:09.951-08:00what is the use of class which is create inside a ...what is the use of class which is create inside a interface...Anonymoushttps://www.blogger.com/profile/01415515329634489774noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-30622229560066212712012-09-04T06:42:01.452-07:002012-09-04T06:42:01.452-07:00@Anonymous, you are write compare() expect Object ...@Anonymous, you are write compare() expect Object so its overloaded, should have used @Override to prevent that, that is very hard to find bug if you are not using @Override annotation. thanks for pointing it. I will correct it.Javin @ CyclicBarrier Example Javahttp://javarevisited.blogspot.sg/2012/07/cyclicbarrier-example-java-5-concurrency-tutorial.htmlnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-72997214190408084042012-09-04T05:34:37.614-07:002012-09-04T05:34:37.614-07:00Is the price comparator working? I tried the code....Is the price comparator working? I tried the code.but it's not behaving as expected.The method compare(SmartPhone sp1, SmartPhone sp2) is overloaded instead of overrididng isn't it?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-69838753008286740842012-05-24T02:38:16.505-07:002012-05-24T02:38:16.505-07:00wow I didn't know that there is a built in Com...wow I didn't know that there is a built in Comparator exists to compare Strings in CASE INSENSITIVE order. that is kind of nice little find for me. on related note what is difference between an ordered collection or Sorted collection, isn't it both the same ? is ArrayList a sorted collection or ordered collection ?Sortinghttp://en.wikipedia.org/wiki/Sortingnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-65328305333554127522012-03-07T21:55:14.804-08:002012-03-07T21:55:14.804-08:00how to sort arraylist in java - Use Collections.so...how to sort arraylist in java - Use Collections.sort<br />how to sort arraylist of objects in java - Use collections.sort with compareTo<br />how to sort arraylist in java using comparator - pass Comparator to sort()<br />how to sort arraylist in descending order in java - use reverseComparator() and pass that to sortAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-82770415677785447922012-03-04T23:48:28.044-08:002012-03-04T23:48:28.044-08:00good example on how to sort arraylist in java but ...good example on how to sort arraylist in java but I guess this method can also be used to sort other List implementation like Vector and LinkedList, isn't it ?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-70507260302436682002012-03-02T00:36:22.115-08:002012-03-02T00:36:22.115-08:00I really like different kind of comparator Java pr...I really like different kind of comparator Java provides like reverse comparator which can be used to sort List in descending order, Case Insensitive Comparator which can be used to Sort array in case insensitive order. Can you please give example of sorting arraylist in two different columns e.g. first name and than surname ?Radhikanoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-19765824386485132512012-01-30T22:26:56.575-08:002012-01-30T22:26:56.575-08:00Should have mention that Object should be implemen...Should have mention that Object should be implemented Comparable interface in order to them to be sorted in Arraylist in natural order. first natural order is applied and than ascending or descending which determine direction.Anonymousnoreply@blogger.com