title: How to sort strings in a structure lexicographically?
body: <p>I'm trying to sort strings according to their names lexicographically.</p>
  
  <p>so I have an array of structures</p>
  
  <pre><code>typedef struct buff{
      char *name;
  } structure;
  </code></pre>
  
  <p>and I'm copying names of files with their associated extensions. So that the content of that structure looks like <code>s[0].name = "picture1.jpg"</code> <code>s[1].name = "DCP003.JPG"</code> and stuff like that.</p>
  
  <p>and I'm trying to sort that, and I'm unable to achieve that.. what I have so far is this.</p>
  
  <pre><code>void sort(structure *s, int counter){
  
      for (int i = 0; i &lt; counter - 1; i++){
          for (int j = 0; j &lt; counter - 1 - i; j++){
  
                  if (strcmp(s[j].name, s[j+1].name) &gt; 0){
  
                      structure tmp;
  
                      tmp = s[j];
                      s[j] = s[j+1];
                      s[j+1] = tmp;
  
              }
          }
      }
  
      for (int i = 0; i &lt; counter; i++){
          printf("%d - %s\n", i+1, s[i].name);
      }
  }
  </code></pre>
  
  <p>and it doesn't work as I want.. tried several versions still no good .. where am I making mistake? Any advice is greatly appreciated..</p>