0

I have the following type of data:

case class TipoDeDato[T] (nombreCampo: String,valor: T)

And in my exercise, I need to create the following structure, using the type of data I mentioned:

Data Structure

So, I created the following structure

  val registro0: List[TipoDeDato[_>: String with Int]] = List(
    new TipoDeDato[String]("Autor", "Gabo"),
    new TipoDeDato[String]("Titulo", "100 Años"),
    new TipoDeDato[Int]("Numero de Paginas", 700)
  )
  val registro1: List[TipoDeDato[_>: String with Int]] = List(
  new TipoDeDato[String]("Autor", "Gabo"),
  new TipoDeDato[String]("Titulo", "Maria"),
  new TipoDeDato[Int]("Numero de Paginas", 1200)
  )
  val registro2: List[TipoDeDato[_>: String with Int]] = List(
    new TipoDeDato[String]("Autor", "Gabo"),
    new TipoDeDato[String]("Titulo", "Carrasco"),
    new TipoDeDato[Int]("Numero de Paginas", 150)
  )
  val registro3: List[TipoDeDato[_>: String with Int]] = List(
    new TipoDeDato[String]("Autor", "Gabo"),
    new TipoDeDato[String]("Titulo", "Oceano"),
    new TipoDeDato[Int]("Numero de Paginas", 200)
  )

And to create the "Libros" object, I have done the following:

val Libros: List[List[TipoDeDato[_>: String with Int]]] = List(registro0,registro1,registro2,registro3)

My question is, how can I sort the "Libros" object, by any of its components, "Autor", "Titulo", "Numero de paginas"?, is this structure adequate for what I need to do?

6
  • Your questions isn't well defined: First, you're sorting different data types (in the example - Int and String - how is the sorting order defined? E.g. is 752 greater than or smaller than "HGabo" ? And second - how is the order of lists of such items defined? longest list to shortest? order of maximum values of lists? Commented Jul 25, 2017 at 1:17
  • Hi, i need sort, by field "new TipoDeDato[Int]("Numero de Paginas", 752)", by example. And I do not know if with this possible sea data structure, or if I have to change it. Commented Jul 25, 2017 at 1:26
  • Hi, I change it. I have List of List, how to sort by "NumeroDePaginas" object? Commented Jul 25, 2017 at 1:33
  • btw, you don't have to do new in order to instantiate case class. TipoDeDato(Autor("HGabo")) is enough Commented Jul 25, 2017 at 13:50
  • also you could make TipoDeDato to be a trait: sealed trait TipoDeDato; case class Autor (autor: String) extends TipoDeDato .... It would simplify your code a lot (especially type signatures) Commented Jul 25, 2017 at 13:51

1 Answer 1

1

To sort List of List:

sealed trait TipoDeDato

case class Autor (autor: String) extends TipoDeDato
case class Titulo (titulo: String) extends TipoDeDato
case class NumeroDePaginas (numeroDePaginas: Int) extends TipoDeDato

class TablaItems(var registros: List[List[TipoDeDato]]){
  def insertInto(reg: List[List[TipoDeDato]]): TablaItems = {
    registros = registros ::: reg
    this
  }
}

  val registro0: List[TipoDeDato] = List(
    Autor("HGabo"), 
    Titulo("ZLa María"),
    NumeroDePaginas(752)
  )

  val registro1: List[TipoDeDato] = List(
    Autor("AGabo"),
    Titulo("CLa María"),
    NumeroDePaginas(521)
  )

  val Registros1: List[List[TipoDeDato]] = List(registro0)
  val Registros2: List[List[TipoDeDato]] = List(registro1)

  val tablaLibros = new TablaItems(Registros1)
  tablaLibros.registros.foreach(println)
  println("----")
  tablaLibros.insertInto(Registros2)
  tablaLibros.registros.foreach(println)
  println("----")
  tablaLibros.registros.sortBy(r=>r.collectFirst{
      case NumeroDePaginas(n) => n
  }.getOrElse(0))

Actually I think you need:

case class Dato(autor: String, titulo: String, numeroDePaginas: Int)


class TablaItems(var registros: List[Dato]){
  def insertInto(reg: List[Dato]): TablaItems = {
    registros = registros ::: reg
    this
  }
}

  //you can also do (if you prefer) `Dato(author = "HGabo", titulo = "ZLa María", numeroDePaginas = 752)
  val registro0 = Dato("HGabo", "ZLa María", 752) 

  val registro1 = Dato("AGabo", "CLa María", 521)

  val Registros1: List[Dato] = List(registro0)
  val Registros2: List[Dato] = List(registro1)

  val tablaLibros = new TablaItems(Registros1)
  tablaLibros.registros.foreach(println)
  println("----")
  tablaLibros.insertInto(Registros2)
  tablaLibros.registros.foreach(println)
  println("----")
  tablaLibros.registros.sortBy(_.numeroDePaginas)

Also, if this problem requires functional programming (no side-effects, and also I got rid of OOP - however the latter is not mandatory, OOP and FP are orthogonal):

case class TablaItems(registros: List[Dato])

implicit class TablaItemsOperations(tabla: TablaItems){
  def withData(reg: List[Dato]) = TablaItems(tabla.registos :: reg)
}

...


val tablaLibros = TablaItems(Registros1)
tablaLibros.registros.foreach(println)
println("----")
val tablaLibrosUpdated = tablaLibros.withData(Registros2)
tablaLibrosUpdated.registros.foreach(println)
println("----")
tablaLibrosUpdated.registros.sortBy(_.numeroDePaginas)
Sign up to request clarification or add additional context in comments.

5 Comments

Wow! You are the best, thank you very much! Please forgive my lousy English :(
No problem! Thanks for the Spanish lesson :)
Ummm and if I want To sort List of List by "Titulo" [String] or "Autor" [String]?
@jamlhet tablaLibros.registros.sortWith(_.autor < _.autor)
@jamlhet you can get more info here: alvinalexander.com/scala/…

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.