1

Writing a function in Scala that accepts an Array/Tuples/Seq of different types of values and sorts it based on first two values in each:

def sortFunction[T](input: Array[T]) = input(0)+ " " + input(1)

The input values I have are as below:

val data = Array((1, "alpha",88.9), (2, "alpha",77), (2, "beta"), (3, "alpha"), (1, "gamma",99))

Then I call the sortFunction as:

data.sortWith(sortFunction)

It is giving below errors:

- polymorphic expression cannot be instantiated to expected type; found : [T]scala.collection.mutable.Seq[T] ⇒ Int required: ((Int, String)) ⇒ ? Error occurred in an application involving default arguments.
- type mismatch; found : scala.collection.mutable.Seq[T] ⇒ Int required: ((Int, String)) ⇒ ? Error occurred in an application involving default arguments.

What am I doing wrong, or how do I get around this? I would be grateful for any suggestions.

2
  • 2
    Your sort function doesn't sort anything. It just takes the first two elements from the Array and tries to create a String out of them. Commented Oct 14, 2016 at 7:43
  • sortfunction does noting but concatenating the given inputs Commented Oct 14, 2016 at 8:10

2 Answers 2

1

If you know type of element in Array[T], you can use pattern matching (when same type). But If You don't know, Program can't decide how to sort your data.

One of methods is just String compare like below.

object Hello{
  def sortFunction[T](input1: T, input2: T) =
    input1 match {
      case t : Product =>
        val t2 = input2.asInstanceOf[Product]
        t.productElement(0).toString < t2.productElement(0).toString
      case v => input1.toString > input2.toString
    }
  def main(args: Array[String]): Unit = {


    val data = Array((1, "alpha",88.9), (2, "alpha",77), (2, "beta", 99), (3, "alpha"), (1, "gamma",99))

    println(data.sortWith(sortFunction).mkString)
  }
}

If you want to know Product tarit, see http://www.scala-lang.org/api/rc2/scala/Product.html

Sign up to request clarification or add additional context in comments.

Comments

1

If you have an Array of tuples that all have the same arity, such as tuples of (Int, String), than your sort function could look something like

def sortFunction[T](fst: (Int, String), scd: (Int, String)) = fst._1 < scd._1 // sort by first element

However, since you have an Array of tuples of varying arity, Scala compiler can only put this under nearest common type Product. Then you can sort like this:

def sortFunction[T](fst: (Product), scd: (Product)) = fst.productElement(1).toString < scd.productElement(1).toString

val data = Array((1, "alpha", 99), (2, "alpha"), (2, "beta"), (3, "alpha"), (1, "gamma"))

data.sortWith(sortFunction) // List((1,alpha,99), (2,alpha), (3,alpha), (2,beta), (1,gamma))

Note that this is really bad design though. You should create an abstract data type that encapsulates your data in a more structured way. I can't say what it should look like since I don't know where and how you are getting this information, but here's an example (called Foo, but you should of course name it meaningfully):

case class Foo(index: Int, name: String, parameters: List[Int])

I just assumed that the first element in each piece of data is "index" and second one is "name". I also assumed that the rest of those elements inside will always be integers and that there may be zero, one or more of them, so hence a List (if it's only zero or one, better choice would be Option).

Then you could sort as:

def sortFunction[T](fst: Foo, scd: Foo) = fst.index < scd.index

or

def sortFunction[T](fst: Foo, scd: Foo) = fst.name < scd.name

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.