title: Sorting a "linked" list in java
body: <p>I have following entity in java:</p>
  
  <pre><code>public class TodoEntity {
     private int id;
    private int previousId;
  }
  
  //Array to sort
  List&lt;TodoEntity&gt; unsorted=new ArrayList&lt;&gt;();
  </code></pre>
  
  <p>What is the fastest way to sort this array so that every previousId matches to the id (respectively an item before/after)</p>
  
  <hr>
  
  <p>Current implementation</p>
  
  <pre><code> public static List&lt;TodoEntity&gt; sortEntites(LinkedList&lt;TodoEntity&gt; todoEntities) {
      boolean isClean=true;
      while(isClean){
          boolean isRoundUnclean=false;
          for(int i=0;i&lt;todoEntities.size();i++){
              TodoEntity todoEntity=todoEntities.get(i);
              if(i&gt;0){
                  TodoEntity todoEntityPrevious=todoEntities.get(i-1);
                  if(todoEntity.getPrevious()!= null &amp;&amp; !todoEntity.getPrevious().equals(todoEntityPrevious.getGuid())){
                      int index=getIndex(todoEntities,todoEntity.getPrevious());
                      todoEntities.remove(i);
                      todoEntities.add(index,todoEntity);
                      isRoundUnclean=true;
                      break;
                  }
                  if(todoEntity.getPrevious()==null){
                      todoEntities.remove(i);
                      todoEntities.add(0,todoEntity);
                  }
              }
          }
          isClean = isRoundUnclean;
      }
      return todoEntities;
  }
  </code></pre>