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
finished already information about Difference between sorted, sortWith and sortBy in Scala if you mers this information useful for you and want to bookmark or share please use link https://19miracles.blogspot.com/2014/07/difference-between-sorted-sortwith-and.html
Tag :
Difference between sorted, sortWith and sortBy in Scala
By
untu
di
Juli 25, 2014
Difference between sorted, sortWith and sortBy in Scala - I made this blog Gadget Miracles already a few years ago, although I am busy working in my office still build this blog for you who need information about gadgets, has very much information that we provide about the Difference between sorted, sortWith and sortBy in Scala which you are looking for at this time, please refer to the information we provide in full guarantee because I have collected a lot of information from the source of the richest:
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
enough already information about Difference between sorted, sortWith and sortBy in Scala
hopefully the information Difference between sorted, sortWith and sortBy in Scala can give you answers to all your questions ayang is conveyed to google.
finished already information about Difference between sorted, sortWith and sortBy in Scala if you mers this information useful for you and want to bookmark or share please use link https://19miracles.blogspot.com/2014/07/difference-between-sorted-sortwith-and.html
Tag :
Tags :
Langganan:
Posting Komentar (Atom)

0 komentar:
Posting Komentar