Calling qsort 3 times will end up sorting the array 3 times. The final sort will take precedence, so the items will end up in branding order - is this what is happening?
You only need to call qsort once, just change your sort callback function to check LayerCode, SubCode and then BrandCode. i.e. if the LayerCode is the same, then check SubCode, if the SubCode is the same, check BrandCode.
Also note that your LayerCode sort was using the usKey field, not the usLayer field.
static int iSortEverything(const void *data1, const void *data2 )
{
stLayerRec_t *pstLayer1;
stLayerRec_t *pstLayer2;
int iDiff;
pstLayer1 = (stLayerRec_t*)data1;
pstLayer2 = (stLayerRec_t*)data2;
iDiff = (int)pstLayer1->usLayerCode - (int)pstLayer2->usLayerCode;
if (iDiff != 0)
return iDiff;
iDiff = (int)pstLayer1->usSubCode - (int)pstLayer2->usSubCode;
if (iDiff != 0)
return iDiff;
iDiff = (int)pstLayer1->usBrandCode - (int)pstLayer2->usBrandCode;
return( iDiff );
}