Max Gadget, GPG Dragon Latest Version, Install Android, Sony Xperia, Xiaomi Mi Robot Vacuum, Verizon, Android Marshmallow, Network, stock firmware

Jumat, 25 Juli 2014

Difference between sorted, sortWith and sortBy in Scala

Difference between sorted, sortWith and sortBy in Scala - this blog we have built a few years ago and already very many blog visitors Max Gadget who are satisfied with the information we convey and we say thanks for that, we will then improve the quality of information we convey to you, well according to what you are looking for we will now discuss first about Difference between sorted, sortWith and sortBy in Scala this information we framework from various trusted sources, please see:

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

    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

    // Sequence of numbers
    val xs = Seq(1, 5, 3, 4, 6, 2)
    // Sort using Natural ordering as defined for Integers in Scala Library
    xs.sorted //1,2,3,4,5,6
    // Sort 'with' a comparator function
    xs.sortWith(_<_) //1,2,3,4,5,6
    xs.sortWith(_>_) //6,5,4,3,2,1
    xs.sortWith((left,right) => left > right) //6,5,4,3,2,1
    // Create a Person class
    case class Person(val name:String, val age:Int)
    // Define a list of Persons
    val ps = Seq(Person("John", 32), Person("Bruce", 24), Person("Cindy", 33), Person("Sandra", 18))
    // Sort People by increasing Age (natural ordering of Int will kick in)
    ps.sortBy(_.age) //List(Person(Sandra,18), Person(Bruce,24), Person(John,32), Person(Cindy,33))
    // Sort People by decreasing Age, using a comparator function
    ps.sortWith(_.age > _.age) //List(Person(Cindy,33), Person(John,32), Person(Bruce,24), Person(Sandra,18))





    so much information Difference between sorted, sortWith and sortBy in Scala

    hopefully the information Difference between sorted, sortWith and sortBy in Scala that we convey can make you satisfied because it can be useful to determine the gadget according to your needs.

    you just read the article titled Difference between sorted, sortWith and sortBy in Scala if you feel this information is useful and want to bookmark or share please use the link https://maxyaquos.blogspot.com/2014/07/difference-between-sorted-sortwith-and.html do not forget to go back to this blog to get more information about gadgets.

    Tag :
    Share on Facebook
    Share on Twitter
    Share on Google+
    Tags :

    Related : Difference between sorted, sortWith and sortBy in Scala

      0 komentar:

      Posting Komentar