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
Difference between sorted, sortWith and sortBy in Scala
By
jesica
Difference between sorted, sortWith and sortBy in Scala - we say welcome to the blog Tecno Android Phones we provide a lot of latest gadget information that must be very dear for you to miss, now we will discuss first about Difference between sorted, sortWith and sortBy in Scala we have collected a lot of information to make this article to complete for you, Please read:
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
Articles about Difference between sorted, sortWith and sortBy in Scala finished discussed
We think it is enough information about Difference between sorted, sortWith and sortBy in Scala, hopefully the information we give can give benefit for you,
If you feel the information Difference between sorted, sortWith and sortBy in Scala that we provide can provide benefits for others please share with link https://southernmatron.blogspot.com/2014/07/difference-between-sorted-sortwith-and.html thank you for visiting our blog page and do not forget to visit other pages.
Tag :
Tags :
Subscribe to:
Post Comments (Atom)
0 komentar:
Post a Comment