title: How to implement maxBy with multiple max in Scala
body: <p>I need a maxBy that returns all max values in case of equality.</p>
  
  <p>Here is the signature and a first implementation :</p>
  
  <pre><code>def maxBy[A, B](as: Seq[A])(f: A =&gt; B)(implicit cmp: Ordering[B]) : Seq[A] = 
    as.groupBy(f).toList.maxBy(_._1)._2
  </code></pre>
  
  <p>Example :</p>
  
  <pre><code>maxBy(Seq(("a", "a1"),("a", "a2"),("b", "b1"),("b", "b2")))(_._1)
  res6: Seq[(String, String)] = List(("b", "b1"), ("b", "b2"))
  </code></pre>
  
  <p>Updated with @thearchetypepaul comment</p>
  
  <pre><code>  def maxBy[A, B](l: Seq[A])(f: A =&gt; B)(implicit cmp: Ordering[B]) : Seq[A] = {
      l.foldLeft(Seq.empty[A])((b, a) =&gt;
        b.headOption match {
          case None =&gt; Seq(a)
          case Some(v) =&gt; cmp.compare(f(a), f(v)) match {
            case -1 =&gt; b
            case 0 =&gt; b.+:(a)
            case 1 =&gt; Seq(a)
          }
        }
      )
    }
  </code></pre>
  
  <p>Is there a better way ?</p>