// function to sort an array of strings

long long stringsort(unsigned char *sarray, long long *nstring, long long *maxlength){
long ii,offset=*maxlength+1,i, s = 1, n=*nstring;
long k;
unsigned char *s1,*s2,*p1,*p2;
unsigned char temp;
    while (s){
      s=0;
      for(i=1;i<n;i++){
        s2=i*offset+sarray;
        s1=(i-1)*offset+sarray;
        ii = *s1 < *s2 ? *s1 : *s2;     //get the smaller  length
        p1 = s1 + 1; p2 = s2 + 1;
        k=0;                            //assume the strings match
        while((ii--) && (k==0)) {
          if(*p1 > *p2){
            k=1;                        //earlier in the array is bigger
          }
          if(*p1 < *p2){
             k=-1;                      //later in the array is bigger
          }
          p1++; p2++;
        }
      // if up to this point the strings match
      // make the decision based on which one is shorter
      if(k==0){
        if(*s1 > *s2) k=1;
        if(*s1 < *s2) k=-1;
      }
      if (k==1){                        // if earlier is bigger swap them round
        ii = *s1 > *s2 ? *s1 : *s2;     //get the bigger length
        ii++;
        p1=s1;p2=s2;
        while(ii--){
          temp=*p1;
          *p1=*p2;
          *p2=temp;
          p1++; p2++;
        }
        s=1;
      }
    }
  }
return k;
}

