Skip to main content
added 124 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237
int sort(book *data, int books, char mode, char sortBy) {

  int i, j;
  book aux;
  aux.cat = malloc(sizeof *(aux.cat));

  if(sortBy == 'n') {
    for(i = 0; i < books; i++) {
      for(j = i; j < books; j++) {
        if(mode == 'a') {
          if(strcmp((data + i)->fname, (data + j)->fname) > 0) {
            aux = *(data + i);
            *(data + i) = *(data + j);
            *(data + j) = aux;
          }
        }
        else {
          if(strcmp((data + i)->fname, (data + j)->fname) < 0) {
            aux = *(data + i);
            *(data + i) = *(data + j);
            *(data + j) = aux;
          }
        }
      }
    }
  }
  else {
    for(i = 0; i < books; i++) {
      for(j = i; j < books; j++) {
        if(mode == 'a') {
          if(strcmp((data + i)->categ, "Fictiune") == 0) {
            if(((data + i)->cat)->price > ((data + j)->cat)->price) {
              aux = *(data + i);
              *(data + i) = *(data + j);
              *(data + j) = aux;
            }
          }
        }
        else {
          if(strcmp((data + i)->categ, "Fictiune") == 0) {
            if(((data + i)->cat)->price < ((data + j)->cat)->price) {
              aux = *(data + i);
              *(data + i) = *(data + j);
              *(data + j) = aux;
            }
          }
        }
      }
    }
  }

  free(aux.cat);

  return 0;
}

I need to sort the data array in two modes: ascending and descending, and by two criteria: name**,name and price.

The question is: is there a way to write another, more compact function that will do the same thing?

int sort(book *data, int books, char mode, char sortBy) {

  int i, j;
  book aux;
  aux.cat = malloc(sizeof *(aux.cat));

  if(sortBy == 'n') {
    for(i = 0; i < books; i++) {
      for(j = i; j < books; j++) {
    if(mode == 'a') {
      if(strcmp((data + i)->fname, (data + j)->fname) > 0) {
        aux = *(data + i);
        *(data + i) = *(data + j);
        *(data + j) = aux;
      }
    }
    else {
      if(strcmp((data + i)->fname, (data + j)->fname) < 0) {
        aux = *(data + i);
        *(data + i) = *(data + j);
        *(data + j) = aux;
      }
    }
      }
    }
  }
  else {
    for(i = 0; i < books; i++) {
      for(j = i; j < books; j++) {
    if(mode == 'a') {
        if(strcmp((data + i)->categ, "Fictiune") == 0) {
          if(((data + i)->cat)->price > ((data + j)->cat)->price) {
        aux = *(data + i);
        *(data + i) = *(data + j);
        *(data + j) = aux;
          }
        }
    }
    else {
      if(strcmp((data + i)->categ, "Fictiune") == 0) {
          if(((data + i)->cat)->price < ((data + j)->cat)->price) {
        aux = *(data + i);
        *(data + i) = *(data + j);
        *(data + j) = aux;
          }
        }
    }
      }
    }
  }

  free(aux.cat);

  return 0;
}

I need to sort the data array in two modes: ascending and descending, and by two criteria: name**, and price.

The question is: is there a way to write another more compact function that will do the same thing?

int sort(book *data, int books, char mode, char sortBy) {

  int i, j;
  book aux;
  aux.cat = malloc(sizeof *(aux.cat));

  if(sortBy == 'n') {
    for(i = 0; i < books; i++) {
      for(j = i; j < books; j++) {
        if(mode == 'a') {
          if(strcmp((data + i)->fname, (data + j)->fname) > 0) {
            aux = *(data + i);
            *(data + i) = *(data + j);
            *(data + j) = aux;
          }
        }
        else {
          if(strcmp((data + i)->fname, (data + j)->fname) < 0) {
            aux = *(data + i);
            *(data + i) = *(data + j);
            *(data + j) = aux;
          }
        }
      }
    }
  }
  else {
    for(i = 0; i < books; i++) {
      for(j = i; j < books; j++) {
        if(mode == 'a') {
          if(strcmp((data + i)->categ, "Fictiune") == 0) {
            if(((data + i)->cat)->price > ((data + j)->cat)->price) {
              aux = *(data + i);
              *(data + i) = *(data + j);
              *(data + j) = aux;
            }
          }
        }
        else {
          if(strcmp((data + i)->categ, "Fictiune") == 0) {
            if(((data + i)->cat)->price < ((data + j)->cat)->price) {
              aux = *(data + i);
              *(data + i) = *(data + j);
              *(data + j) = aux;
            }
          }
        }
      }
    }
  }

  free(aux.cat);

  return 0;
}

I need to sort the data array in two modes: ascending and descending, and by two criteria: name and price.

The question is: is there a way to write another, more compact function that will do the same thing?

edited title
Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

Compress Shorten a sorting function

deleted 21 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

So I have this function:

I need to sort the *datadata array in two modes: ascending,ascending and descendingdescending, and by two criteriascriteria: namename**, and priceprice.

And, what do you think about line 5 (aux.cat = malloc(sizeof *(aux.cat));aux.cat = malloc(sizeof *(aux.cat));)? It'sIs it a right way to initialize *catthe cat pointer?

ThisThese are the structures:

So I have this function:

I need to sort the *data array in two modes: ascending, and descending, and by two criterias: name, and price.

And, what do you think about line 5 (aux.cat = malloc(sizeof *(aux.cat));)? It's a right way to initialize *cat pointer?

This are the structures:

I have this function:

I need to sort the data array in two modes: ascending and descending, and by two criteria: name**, and price.

And, what do you think about line 5 (aux.cat = malloc(sizeof *(aux.cat));)? Is it a right way to initialize the cat pointer?

These are the structures:

edited tags
Link
200_success
  • 144.2k
  • 22
  • 188
  • 473
Loading
Source Link
mariusmmg2
  • 261
  • 3
  • 7
Loading