Difference between sorted, sortWith and sortBy in Scala

Difference between sorted, sortWith and sortBy in Scala - welcome to our blog Techno Live, now we will discuss information about the Difference between sorted, sortWith and sortBy in Scala, our admins on this blog has been around to collect information you are looking for so that we usakan to display complete information for you, and in this blog, you can also search for other information, ok please continue reading may be easily understood:

This is about : Difference between sorted, sortWith and sortBy in Scala
And this article : Difference between sorted, sortWith and sortBy in Scala

You can also see our article on:


Difference between sorted, sortWith and sortBy in Scala

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





Articles Difference between sorted, sortWith and sortBy in Scala finished we discussed

A few of our information about the Difference between sorted, sortWith and sortBy in Scala, I hope you can exploit carefully

No've You've finished reading an article on Difference between sorted, sortWith and sortBy in Scala and many articles about modern home in our blog this, please read it. and url link of this article is https://liveeconcerts.blogspot.com/2014/07/difference-between-sorted-sortwith-and.html Hopefully discussion articles on provide more knowledge about the world of tech gadgets.

Tag :

0 Response to "Difference between sorted, sortWith and sortBy in Scala"

Post a Comment