title: Sorting list according to previous order
body: <p>In code below the <code>List</code> ordering of <code>val originalOrdering</code> should be re-created after removing element(s) from the <code>List</code>.</p>
  
  <p>If <code>val random3ElementStringList</code> takes value <code>List(3, 4, 1)</code> then List should be re-ordered to <code>List(1 , 3 , 4)</code>
  The original list <code>val originalOrdering</code> with values <code>1,2,3,4</code> does not suggest the values that can be contained in the <code>List</code>. The list can contain characters other than numeric.</p>
  
  <pre><code>import scala.collection.immutable._
  
  object sort extends App {
  
      val originalOrdering : List[String] = List("1" , "2" , "3" , "4");
  
      var random3ElementStringList : List[String] = util.Random.shuffle(originalOrdering).take(3)
  
      val stringPositions : Map[String , Int] = originalOrdering.zipWithIndex.toMap
  
      println(random3ElementStringList)
  
      println(stringPositions);
  
      val fl = random3ElementStringList.foldLeft("")((a , b) =&gt; (stringPositions.get(b)+","))
  
      println(fl)
  
  }
  </code></pre>
  
  <p>The output of this code is : </p>
  
  <pre><code>List(2, 3, 4)
  Map(1 -&gt; 0, 2 -&gt; 1, 3 -&gt; 2, 4 -&gt; 3)
  Some(3),
  </code></pre>
  
  <p>But <code>fl</code> should contain the sentences in sorted order where the sort is determined by the original order in <code>stringPositions</code></p>
  
  <p>Update : </p>
  
  <pre><code>import scala.collection.immutable._
  
  object sort extends App {
  
      def reOrder(originalList : List[String] , originalListSubset : List[String]) = {
  
          val stringPositions : Map[String , Int] = originalList.zipWithIndex.toMap
          val fl : List[(String , Int)] = originalListSubset.map(m =&gt; (m , stringPositions.get(m).get))
  
          fl.sortBy(s =&gt; s._2).map(m =&gt; m._1)
      }
  
      val originalList : List[String] = List("a" , "b" , "c" , "f");
      var random3ElementStringList : List[String] = util.Random.shuffle(originalList).take(3)
  
      println(random3ElementStringList)
      println(reOrder(originalList , random3ElementStringList))
  
      random3ElementStringList = util.Random.shuffle(originalList).take(2)
  
      println(random3ElementStringList)
      println(reOrder(originalList , random3ElementStringList))
  }
  </code></pre>