简体   繁体   中英

using qsort on a nested struct array

Below are my Structures and qsort declaration (to be arranged based on nID)

EDIT:

struct items{
    int nID;
    int nQuantity;
    int nItems;
    float fPrice;
    char cItemName[21];
    char cCategory[21];
    char cItemDesc[31];
};

struct Register{
    struct items List[20];
    char cID[11];
    char cPassword[11];
    char cAddress[31];
    char cContact[16];
    char cName[21];
}; 

int main ()
{
  /*These are the contents of List[]*/
  Users[nInd].List[1].nID = 1;
  strcpy(Users[nInd].List[1].cItemName, "iPhoneMAXMAX");
  strcpy(Users[nInd].List[1].cCategory, "Gadgettronss");
  strcpy(Users[nInd].List[1].cItemDesc, "This is an iphone");
  Users[nInd].List[1].nQuantity = 1;
  Users[nInd].List[1].fPrice = 100;

  Users[nInd].List[0].nID = 50;
  strcpy(Users[nInd].List[0].cItemName, "iPhone");
  strcpy(Users[nInd].List[0].cCategory, "Gadgets");
  strcpy(Users[nInd].List[0].cItemDesc, "This is an iphone");
  Users[nInd].List[0].nQuantity = 20;
  Users[nInd].List[0].fPrice = 50;

  for (i = 0; i < 2; i++)
    qsort (&Users[nInd].List[i], 2, sizeof (struct Register), sort);

  for (i = 0; i < 2; i++)
    printf ("%11d  %20s\t %15s  \t   % 10.2f  \t\t      %2d\n", 
        Users[nInd].List[i].nID, Users[nInd].List[i].cItemName,
        Users[nInd].List[i].cCategory, Users[nInd].List[i].fPrice, 
        Users[nInd].List[i].nQuantity);
/*rest of the code*/
}

Ideal Output:

Product ID Item Name Category Price Quantity

1 Iphone Gadgets 50.00 20

50 IphoneMAXMAX Gadgettronss 100.00 1

Actual Ouput:

50 IphoneMAXMAX Gadgettronss 100.00 1

1 Iphone Gadgets 50.00 20

However, my problem is that when I display the contents of List[] nothing changes.

This is my comparator function for qsort:

int sort (const void*p, const void*q)
{
  const struct Register *ip = (struct Register*)p;
  const struct Register *iq = (struct Register*)q;

  if (ip->List[0].nID > iq->List[1].nID)
    return 1;
  else if (ip->List[0].nID < iq->List[1].nID)
    return -1;
  else
    return 0;

}

If you want to sort the elements in the List array using qsort , then do eg

qsort(Users[nInd].List, NumberOfElementsInList, sizeof(struct items), CompareItems);

This will sort the NumberOfElementsInList first struct items elements in the array Users[nInd].List .

Your comparison function receives pointers to the struct items elements in the array:

int CompareItems(const void *a, const void *b)
{
    const struct items *item_a = (const struct items *) a;
    const struct items *item_b = (const struct items *) b;

    if (item_a->nID > item_b->nID)
        return 1;
    else if (item_a->nID < item_b->nID)
        return -1;
    else
        return 0;
}

If you want to "sort" only the two first elements in the Users[nInd].List array then there's no need for the qsort function, just compare the two elements directly and swap if needed:

if (Users[nInd].List[0].nID > Users[nInd].List[1].nID)
{
    // Swap the items as index 0 and 1, thereby sorting them
    struct items temp_item = Users[nInd].List[0];
    Users[nInd].List[0] = Users[nInd].List[1];
    Users[nInd].List[1] = temp_item;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM