Scala collections provide you three options for sorting: sorted( ), sortWith( ) and sortBy( ). Here is a simplified explanation:
sorted
Will sort the list using the natural ordering (based on the implicit Ordering passed)
sortBy (an attribute)
Sort by a given attribute using the attribute's type.
e.g. given a list of Person objects, if you want to sort them in ascending order of their age (which is an Int), you could simply say: personList.sortBy(_.age)
sortWith (a function)
Takes a comparator function. Useful when you want to specify a custom sorting logic.
e.g. if you want to sort by age descending, you could write this as:
personList.sortWith{(leftE,rightE) =>
leftE.age > rightE.age
}
Or, more simply: personList.sortWith(_.age > _.age)
Checkout this gist for a full example:
https://gist.github.com/gsluthra/80555ed4af24bea244b5
Info Difference between sorted, sortWith and sortBy in Scala
By
Unknown
Info Difference between sorted, sortWith and sortBy in Scala - this blog we have built from a few years ago and already very much information about gadgets that we convey and a lot of blog visitors New Blog Techno News who are satisfied with the information, we will always try to update the latest information for you, first about Info Difference between sorted, sortWith and sortBy in Scala many already we collect data to make this article so you do not miss the news, please see:
Articles : Info Difference between sorted, sortWith and sortBy in Scala
full Link : Info Difference between sorted, sortWith and sortBy in Scala
Articles : Info Difference between sorted, sortWith and sortBy in Scala
full Link : Info Difference between sorted, sortWith and sortBy in Scala
You can also see our article on:
Info Difference between sorted, sortWith and sortBy in Scala
articles Info Difference between sorted, sortWith and sortBy in Scala finished in discussion
hopefully the information we convey about Info Difference between sorted, sortWith and sortBy in Scala can benefit you in getting new knowledge about technology,
you just finished reading the information Info Difference between sorted, sortWith and sortBy in Scala we hope this informmation can answer the question you submit to googlle, if you want to bookmark or share please use link https://moymoycikukecik.blogspot.com/2014/07/info-difference-between-sorted-sortwith.html and do not forget to always visit this blog to get the latest information every day.
Tag :
Tags :
Langganan:
Posting Komentar (Atom)
0 komentar:
Posting Komentar