Nithin Chandy Nithin Chandy -4 years ago 67
Scala Question

SortWith and custom comparator function

sortKey match {
case SortKeys.ActualUnits => {
ResponseList.sortWith {
(_.aggregations.ActualUnits.get, _.aggregations.ActualUnits.get) =>
compare(_.aggregations.ActualUnits.get,
_.aggregations.ActualUnits.get,
true)
}
}
}


def compare(a: BigDecimal, b: BigDecimal, sortOrder: Boolean) = {
sortOrder match {
case true => a < b
case false => b > a
}
}


aggregations is a caseclass.

Trying to use a custom comparator function to avoid defining two statements for each sortKey.

Compiler throws
error: not a legal formal parameter
.

Answer Source
 (_.aggregations.ActualUnits.get, _.aggregations.ActualUnits.get) =>
            compare(_.aggregations.ActualUnits.get,
                _.aggregations.ActualUnits.get,
                true)

Not really sure what you're trying to do here, but _.aggregations.ActualUnits.get, as the error clearly says, is not a a legal formal paramater. I imagine you meant

 (x, y) =>
            compare(x.aggregations.ActualUnits.get,
                y.aggregations.ActualUnits.get,
                true)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download