title: Order the list based on order of another list
body: <p>I have two lists where the order of one list is different from the order of other list, in the list I am adding a map where key order is different in both lists. I want the order of both the list same.  How can I perform this?</p>
  
  <pre><code>Map&lt;Long, String&gt; attfieldMap = null;
  attfieldMap = view.getNameMap(form);
  
  List&lt;Field&gt; auditMap = new ArrayList&lt;Field&gt;();
  if (itemId != 0) {
  
      Item dom = getRecord();
  
      Map&lt;FormField, Object&gt; tempValues = dom.getValues();
      List&lt;Field&gt; oldValues = new ArrayList&lt;Field&gt;();
  
      for (Map.Entry&lt;FormField, Object&gt; values : tempValues
              .entrySet()) {
          Field oldFld = new Field();
          Form form = (Form) values.getKey();
  
          if (attfieldMap.get(form.getField().getId()) != null) {
              oldFld.setKey(attfieldMap.get(form.getField()
                      .getId()));
              oldFld.setValue(values.getValue());
  
              oldValues.add(oldFld);
          }
      }
  }
  
  
  for (Map.Entry&lt;Long, String&gt; attfieldEntry : attfieldMap.entrySet()) {
      // Mandates to declare within loop
      Field attributeFld = new Field;
  
      attributeFld.setKey(attfieldEntry.getValue());
      attributeFld.setValue(String.valueOf(attfieldEntry.getKey()));
  
      auditMap.add(attributeFld);
  }
  </code></pre>
  
  <p>The list <code>auditMap</code> has map with key <code>att13</code>, <code>att12</code>, <code>att14</code> and <code>oldValues</code> has map with key <code>att12</code>, <code>att13</code>, <code>att14</code>. I need this list to be in same order as <code>auditMap</code> list.  How to order this?</p>
  
  <p>As per the answeres the below code edited :Here the data gets copied,iwant just the sort,even though the list size is different.list1 should have its own data,list2 should have same data,but the order should be maintained</p>
  
  <pre><code>List&lt;Field&gt; auditMap = new ArrayList&lt;Field&gt;();
              attfieldMap = View
                      .getFieldID(form);
  
              for (Map.Entry&lt;Long, String&gt; attfieldEntry : attfieldMap.entrySet()) {
  
                  Field attributeFld = new Field();
  
                  attributeFld.setKey(attfieldEntry.getValue());
                  attributeFld.setValue(String.valueOf(attfieldEntry.getKey()));
  
                  auditMap.add(attributeFld);
                  attributeFld = null;
              }
  
              if (itemId != 0) {
  
                  Item dom = getRecord(domainItem);
  
                  Map&lt;FormField, Object&gt; tempValues = dom.getValues();
                  List&lt;Field&gt; oldValues = new ArrayList&lt;Field&gt;();
  
                  for (Map.Entry&lt;FormField, Object&gt; values : tempValues
                          .entrySet()) {
                      Field oldFld = new Field();
                      Form form = (Form) values.getKey();
  
                      if (attfieldMap.get(form.getField().getId()) != null) {
                          oldFld.setKey(attfieldMap.get(form.getField()
                                  .getId()));
                          oldFld.setValue(values.getValue());
  
                          oldValues.add(oldFld);
                      }
  
                  }
                  final Map&lt;Field, Integer&gt; indices = new HashMap&lt;Field, Integer&gt;();
                  for (int i = 0; i &lt; oldValues.size(); i++) {
                      indices.put(oldValues.get(i), i);
                  }
                  Collections.sort(auditMap, new Comparator&lt;Field&gt;() {
                      public int compare(Field o1, Field o2) {
                          int index1 = indices.containsKey(o1) ? indices.get(o1)
                                  : -1;
                          int index2 = indices.containsKey(o2) ? indices.get(o2)
                                  : -1;
                          return index1 - index2;
                      }
                  });
                  for (int i = 0; i &lt; oldValues.size(); i++) {
                      LOGGER.info("the new data is--" + oldValues.get(i).getKey());
                  }
                  for (int i = 0; i &lt; auditMap.size(); i++) {
                      LOGGER.info("the new data is--" + auditMap.get(i).getKey());
                  }
  </code></pre>
  
  <p>This is my actual code,the data is not getting ordered.The keys in auditMap are 1,2,3,4 and in old values are 1,3,2 the result of old values should be 1,2,3 but its not happening</p>