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
Jumat, 25 Juli 2014
You have finished reading Difference between sorted, sortWith and sortBy in Scala and many articles about Techno News in our blog this, please read it. and url link of this article is https://ikkemunandar.blogspot.com/2014/07/difference-between-sorted-sortwith-and.html Hopefully discussion articles on provide more knowledge about the world of new tech gadgets and Techno News.
Tag :
Difference between sorted, sortWith and sortBy in Scala
Diposting oleh
Unknown
di
23.43
Difference between sorted, sortWith and sortBy in Scala - is the information you are looking for in search engines like google and others, okay we have provided such information in the blog Techno News, we have around collecting information from various reliable sources and presenting it in this blog, well please read Difference between sorted, sortWith and sortBy in Scala to finish:
Articles : Difference between sorted, sortWith and sortBy in Scala
full Link : Difference between sorted, sortWith and sortBy in Scala
Articles : Difference between sorted, sortWith and sortBy in Scala
full Link : Difference between sorted, sortWith and sortBy in Scala
You can also see our article on:
Difference between sorted, sortWith and sortBy in Scala
Information about the Difference between sorted, sortWith and sortBy in Scala has been discussed
A few of our information about the Difference between sorted, sortWith and sortBy in Scala, hopefully give more knowledge for you
You have finished reading Difference between sorted, sortWith and sortBy in Scala and many articles about Techno News in our blog this, please read it. and url link of this article is https://ikkemunandar.blogspot.com/2014/07/difference-between-sorted-sortwith-and.html Hopefully discussion articles on provide more knowledge about the world of new tech gadgets and Techno News.
Tag :
Tags :
Langganan:
Posting Komentar (Atom)
0 komentar:
Posting Komentar