2021年5月6日星期四

How does sortWith in Scala work in terms of iterating a tuple?

A list can be iterated as follows:

scala> val thrill = "Will" :: "fill" :: "until" :: Nil  val thrill: List[String] = List(Will, fill, until)  scala> thrill.map(s => s + "y")  val res14: List[String] = List(Willy, filly, untily)  

The above code first creates a list and then the second command creates a map with an iterable called as 's', the map creates a new string from 's' by appending the char 'y'.

However, I do not understand the following iteration process:

scala> thrill.sortWith((s,t) => s.charAt(0).toLower < t.charAt(0).toLower)  val res19: List[String] = List(fill, until, Will)  

Does the tuple (s,t) take two elements of thrill at once and compares them? How is sorting performed exactly using this syntax/function?

https://stackoverflow.com/questions/67428304/how-does-sortwith-in-scala-work-in-terms-of-iterating-a-tuple May 07, 2021 at 10:36AM

没有评论:

发表评论