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
Friday, July 25, 2014
articles Difference between sorted, sortWith and sortBy in Scala we have conveyed to you, if feel this information is useful and you want to bookmark or share so that more people who know please use link https://profsmythe.blogspot.com/2014/07/difference-between-sorted-sortwith-and.html.
Tag :
Difference between sorted, sortWith and sortBy in Scala
By
untu
at
July 25, 2014
Difference between sorted, sortWith and sortBy in Scala - Did you know that every day there is always a new technology that is created? if you want to know please refer to the blog Gadget News well now we will discuss first about Difference between sorted, sortWith and sortBy in Scala as you need now, we have compiled this article carefully, so please see 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
article Difference between sorted, sortWith and sortBy in Scala has been completed in the discussion
hopefully the information Difference between sorted, sortWith and sortBy in Scala that we provide can be useful for you to know the development of technology in the present
articles Difference between sorted, sortWith and sortBy in Scala we have conveyed to you, if feel this information is useful and you want to bookmark or share so that more people who know please use link https://profsmythe.blogspot.com/2014/07/difference-between-sorted-sortwith-and.html.
Tag :
Tags :
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment