Sort a struct

Asked

Viewed 1,770 times

0

Do you have any way of ordering a struct in the form:

struct cidadecoord
{
  char cidade[90];
  int coordx, coordy;
}cidades[N];

N is worth 30 in the case, and I would like to order x and y coordinates, to find out which cities were further east, west, north and south. I’m struggling because I don’t know how to order keeping the information of the name of the city of each coordinate.

  • Everything depends on the order of your ordering. How do you want to order? In which priority? First x, then y? A distance, ie sqrt(x 2 + y 2), and then the name? Or the name comes first?

  • @rodrigogq In case, I would need to sort x and y separately. The values of x ordered would give me the city farthest west (the lowest value of x) farther east (the highest value of x) and with the values of y ordered I would know the city farther north and farther south, likewise.

  • Do you need to order or just find this information? If it is not necessary to order !

  • @I thought about ordering, because I thought it would be easier to find the highest and lowest value.

2 answers

3


If the idea is just to take the cities further north, south, east and west, can do so:

int indNorte = 0;
int indSul = 0;
int indLeste = 0;
int indOeste = 0;

for(int i = 0; i < N; i++)
{
    if(cidades[indNorte].coordy < cidades[i].coordy) indNorte = i;
    if(cidades[indSul  ].coordy > cidades[i].coordy) indSul = i;
    if(cidades[indLeste].coordx < cidades[i].coordx) indLeste = i;
    if(cidades[indOeste].coordx > cidades[i].coordx) indOeste = i;
}
  • You need to go through all the points necessarily. Sorting through multiple layers will be worse. Here you already get the index of cities without having to order everything.

  • I would need to create other structs to indNorte, indSul, indLeste and indOthis or that way you did it is already enough?

  • It depends.. if you’re going to use it to display cities, you can directly use this index to pick up by name.. Example, city name further north will be cidades[indNorte].cidade. If you are going to return from a function, you can do a struct or something similar (like an array of indices, each representing more to the north, south, east and west). Going to taste.

  • Got it! Thank you

1

Uses the function qsort() of the standard library with its own sorting function

int orderbyx(const void *a, const void *b) {
    const struct cidadecoord *aa = a;
    const struct cidadecoord *bb = b;
    return aa->coordx - bb->coordx;
}
int orderbyy(const void *a, const void *b) {
    const struct cidadecoord *aa = a;
    const struct cidadecoord *bb = b;
    return aa->coordy - bb->coordy;
}

/* ... */
qsort(cidades, N, sizeof *cidades, orderbyx);
/* cidades[0] tem a cidade com x menor */
/* cidades[N-1] tem a cidade com x maior */

/* ... */
qsort(cidades, N, sizeof *cidades, orderbyy);
/* cidades[0] tem a cidade com y menor */
/* cidades[N-1] tem a cidade com y maior */

Browser other questions tagged

You are not signed in. Login or sign up in order to post.