title: Manually sort a linked list in the C programming language
body: <p>How do you sort a linked list by name in a function in C?</p>
  
  <pre><code>struct rec{
      char name[20];
      int nr;
      struct rec *nextRec;
  };
  typedef struct rec Rec; /* synonym for struct rec */
  typedef Rec *RecPtr; /* synonym for pointer */
  
  void SortLinkedList(RecPtr *sPtr, int p_size);/* prototype */
  int main() {
      RecPtr startPtr = NULL;
      /* filling upp the linked list... size = nr of nodes in list */
      SortLinkedList(&amp;startPtr, size);
  }
  
  void SortLinkedList(RecPtr *sPtr, int p_size){
      int i, j;
      RecPtr tempPtr;
      RecPtr currentPtr;
      RecPtr nextPtr;
      currentPtr = *sPtr;
      nextPtr = currentPtr-&gt;nextRec;
      for( j = 0; j &lt;= p_size; j++) { /* loop for nr of nodes */
          for(i = 0; i &lt;= p_size -1 ; i++) { /* loop for one less than nr of nodes */
              if(strcmp(currentPtr-&gt;name, nextPtr-&gt;name) &lt; 0) { /* it less than ...*/
                  tempPtr = currentPtr;/* ...swap with temp */
                  currentPtr = nextPtr; /* but this sorting doesn'nt work */
                  nextPtr = tempPtr;
              }
             currentPtr = nextPtr;
             currentPtr = currentPtr-&gt;nextRec;
          }
      }
  }
  </code></pre>