import scala.xml._

case class Example(name: String, tpe: String, cat: String)

val lst = List(
  Example("black", "2", "A"),
  Example("red", "1", "B"),
  Example("orange", "1", "A"),
  Example("yellow", "2", "A"),
  Example("green", "1", "C"),
  Example("blue", "2", "C"),
  Example("violet", "2", "A"),
  Example("white", "1", "B"))

def doTypes(in: List[Example]) = {
  val types = in.map(_.tpe).removeDuplicates.sort(_ < _)
  <ol>
  {
    types.map(
      t =>
	<li>Type {t}
      <ol>
      {
	in.filter(_.tpe == t).sort(_.name < _.name).map(e => <li>{e.name}</li>)
      }
      </ol>
      </li>
    )
  }
  </ol>
}

def doCats(in: List[Example]) = {
  val cats = in.map(_.cat).removeDuplicates.sort(_ < _)

  <ol>
  {
    cats.map(
      cat =>
	<li>Category {cat}
      {doTypes(in.filter(_.cat == cat))}
      </li>
    )
  }
  </ol>
}

println(doCats(lst))
