I’ve got a list. but the type is any.
// list: List[List[Any]] = List(List(b, 50), List(a, 25), List(i, 60))
val list = List( List("b", 50), List("a", 25), List("i", 60))
// sort should be list(0)(0) "String" sort
("a", 25), ("b", 50), ("i", 60)
// or list(0)(1) "Integer" sort
("a", 25), ("b", 50), ("i", 60)
I want to sort. How can I do? Thank you in advance.
Trying to sort a list like this is inherently unsafe since the compiler cannot guarantee that the elements of the inner lists have the correct types. Perhaps the safest way to accomplish this would be do use a
matchto convert the particular element, and to raise an exception is the data is not what is should be.That said, it sounds like what you really need is a list of
(String, Int)tuples. That way the compiler can guarantee safety:Notice that the compiler knows the types of the inner elements. So sorting is much easier, and safer: