title: Sorting linked list with multiple elements
body: <p>I have this csv file:</p>
  <pre><code>NAME, NUMBER, ADDRESS, EMAIL
  Kevin, +62 812-xxx-xxx, Jln.Anggrek Merah 3, kevin@gmail.com
  Adwi, +62 821-xxxx-xxxx, Jln.Ruhui Rahayu, adwi@gmail.com
  Wasis, +62 813-xxxx-xxxx, Jln.Pramuka 6 25, wasis@gmail.com
  Alief, +62 811-xxxx-xxx, Jln.Padat Karya, alief@gmail.com
  </code></pre>
  <p>This is my code:</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  #define MAX 50
  
  typedef struct Contact
  {
      char name[MAX];
      char number[MAX];
      char address[MAX];
      char email[MAX];
      struct Contact *next;
  } 
  Contact;
  
  void create_linkedList(Contact **start, Contact p)
  {
      Contact *new = malloc(sizeof(Contact));
      strcpy(new-&gt;name, p.name);
      strcpy(new-&gt;number, p.number);
      strcpy(new-&gt;address, p.address);
      strcpy(new-&gt;email, p.email);
  
      new-&gt;next = *start;
      *start = new;
  }
  
  void printList(Contact *start)
  {
      Contact *cursor = start;
      while (cursor != NULL)
      {
          printf(&quot;%s\n&quot;, cursor-&gt;name);
          cursor = cursor-&gt;next;
      }
  }
  
  void sorting(Contact *start)
  {
      Contact *cursor = start, *traverse = NULL;
      Contact *tmp, *tmp_next;
          
      while (cursor != NULL)
      {
          traverse = cursor;
  
          while (traverse-&gt;next != NULL)
          {
              if (strcasecmp(traverse-&gt;name, traverse-&gt;next-&gt;name) &gt; 0)
              {
                  tmp = traverse;
                  tmp_next = traverse-&gt;next-&gt;next;
      
                  traverse-&gt;next = tmp;
  
                  traverse-&gt;next-&gt;next = tmp_next;
              }
              traverse = traverse-&gt;next;
          }
          printf(&quot;Prepare!\n&quot;);
          cursor = cursor-&gt;next;
      }
  }
  
  int main(void)
  {
      FILE *file = fopen(&quot;contacts.csv&quot;, &quot;r&quot;);
      if (file == NULL)
          return 1;
  
      Contact person;
      Contact *start = NULL;
  
      // Loop inside csv file til' the end of file
      while(fscanf(file, &quot;%[^,], %[^,], %[^,], %[^\n] &quot;, person.name, person.number, person.address, person.email) == 4)
      {   
          # Skip header file from csv
          if (strcmp(&quot;NAME&quot;, person.name) == 0) 
              continue;
          
          create_linkedList(&amp;start, person);
      }    
  
      printList(start);
  
      // Swapped
      sorting(start);
  
      printList(start);
  
      return 0;
  }
  </code></pre>
  <p>So, after i successfully created a linked list that connect each person data in my csv file, i want to sort it by their name and then print it. But if you compile my code, it causes segmentation fault.</p>
  <p>In my <code>sort()</code> function i try to change the <code>node (next)</code> of each person. Because i think if i only swap the value of each element, the <code>node (next)</code> will still point same person as before. So i was thinking maybe i can only swap the <code>node (next)</code>.</p>
  <p>I can do it if it's only sorting value in an array. But linked list it's difficult for me as beginner.</p>
  <p>Can you help me, please? Maybe write me some new code and explain the solution, if you guys have it. Thanks!</p>