2
\$\begingroup\$
 def sort[A, B: Ordering](data: List[A], desc: Boolean)(measure: A => B): List[A] = {
        val baseOrdering = Ordering.by(measure)
        val ordering = if (desc) baseOrdering.reverse else baseOrdering
        data.sorted(ordering)   
    }

  private def sortOne(stats: List[One], by: String, desc: Boolean): List[One] =
    by match {
      case "good" => sort(stats, desc)(_.data.good)
      case "bad" => sort(stats, desc)(_.data.bad)
      case "gross" => sort(stats, desc)(_.data.gross)
      case "warn" => sort(stats, desc)(_.data.warn)
      case "exuberant" => sort(stats, desc)(_.data.exuberant)
      case other => stats
    }
  private def sortTwo(stats: List[Two], by: String, desc: Boolean): List[Two] =
    by match {
      case "good" => sort(stats, desc)(_.data.good)
      case "bad" => sort(stats, desc)(_.data.bad)
      case "gross" => sort(stats, desc)(_.data.gross)
      case "warn" => sort(stats, desc)(_.data.warn)
      case "exuberant" => sort(stats, desc)(_.data.exuberant)
      case other => stats
    }

  private def sortThird(stats: List[Third], by: String, desc: Boolean): List[Third] =
    by match {
      case "good" => sort(stats, desc)(_.data.good)
      case "bad" => sort(stats, desc)(_.data.bad)
      case "gross" => sort(stats, desc)(_.data.gross)
      case "warn" => sort(stats, desc)(_.data.warn)
      case "exuberant" => sort(stats, desc)(_.data.exuberant)
      case other => stats
    }

Actually there are more classes. They all have same fields like good, gross, bad, warn.

These classes that are in methods have all similar fields good, gross, bad, warn. I can't rewrite classes.

It is working, but it looks bad. Maybe someone has some ideas?

\$\endgroup\$

closed as off-topic by Donald.McLean, Mast, mdfst13, Vogel612, Mathias Ettinger May 30 '16 at 7:27

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ Assuming the fields are numeric, you can get rid of the desc match by doing data.sortBy ( if desc _.good else - _.good ) stackoverflow.com/a/7803042/14955 \$\endgroup\$ – Thilo May 27 '16 at 12:43
  • 7
    \$\begingroup\$ Welcome to Code Review. This is a fine question, but you could add a small description of what this code is doing and what you want as a review. \$\endgroup\$ – Marc-Andre May 27 '16 at 13:57
  • 7
    \$\begingroup\$ This is a borderline question for Code Review. One, Two, and Third look like fake placeholders for some class names. What are the real classes, and what do they look like? Please tell us so that we can advise you properly. \$\endgroup\$ – 200_success May 27 '16 at 16:49
11
\$\begingroup\$

You could try that:

def sort[A, B: Ordering](data: List[A], desc: Boolean)(measure: A => B): List[A] = {
    val baseOrdering = Ordering.by(measure)
    val ordering = if (desc) baseOrdering.reverse else baseOrdering
    data.sorted(ordering)   
}

Then you do

sort(listOfFirstClass, desc)(_.good)
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.