如何在C中按结构变量对数组进行排序?

huangapple go评论67阅读模式
英文:

How to sort an array** by a struct variable in C?

问题

以下是您要翻译的部分:

我最近一直在尝试使用qsort函数对数组中的结构进行排序。我从.csv文件加载了一个数组**。我的结构如下:

typedef struct {
	int id;
	char regionName[25];
	char townName[25];
	int countOfMan;
	int countOfWoman;
} tTown;

我想按照地区中的女性数量对数组进行排序。我已经定义了比较函数并尝试进行排序。

int compareWomenCount(const tTown* village1, const tTown* village2) {
	int result = 0;
	if (village1->countOfWoman < village2->countOfWoman) {
		result = -1;
	}
	else if (village1->countOfWoman > village2->countOfWoman) {
		result = 1;
	}
	return result;
}

这是我的qsort函数:

void sortArray(tTown** region, int arrayLength) {
    qsort(region, arrayLength, sizeof(tTown*), compareWomenCount);
}

但它没有起作用。有人知道该怎么办吗?

谢谢!

英文:

I have been recently up to a sorting structures in an array by a function qsort. I loaded an array** from .csv file. My structure looks like this.

typedef struct {
	int id;
	char regionName[25];
	char townName[25];
	int countOfMan;
	int countOfWoman;
} tTown;

I want to sort an array by amount of women in region. I have defined compare function and tried to sort it.

int compareWomenCount(const tTown* village1, const tTown* village2) {
	int result = 0;
	if (village1-&gt;countOfWoman &lt; village2-&gt;countOfWoman) {
		result = -1;
	}
	else if (village1-&gt;countOfWoman &gt; village2-&gt;countOfWoman) {
		result = 1;
	}
	return result;
}

This is my qsort:

void sortArray(tTown** region, int arrayLength) {
    qsort(region, arrayLength, sizeof(tTown*), compareWomenCount);
}

But it didn't work. Has anyone a clue, what to do?

Thank you!

答案1

得分: 0

The qsort 函数将数组的每个元素的指针传递给比较函数(基本上它在每个元素上使用地址操作符 &amp;,就像 &amp;region[i])。

如果数组中的每个元素都是指针,那么传递的是一个指向指针的指针。

这意味着您的比较函数应该如下所示:

int compareWomenCount(const tTown** village1, const tTown** village2) {
    int result = 0;
    if ((*village1)->countOfWoman < (*village2)->countOfWoman) {
        result = -1;
    }
    else if ((*village1)->countOfWoman > (*village2)->countOfWoman) {
        result = 1;
    }
    return result;
}

此外,排序函数的参数需要是通用的常量指针(const void *)以确保正确,然后在函数内部将它们强制转换为正确的指针类型。

英文:

The qsort function passes a pointer to each element of the array to the comparison function (basically it uses the address-of operator &amp; on each element, as in &amp;region[i]).

If each element in the array is a pointer, what is passed is a pointer to a pointer.

That means your comparison function will need to look like

int compareWomenCount(const tTown** village1, const tTown** village2) {
    int result = 0;
    if ((*village1)-&gt;countOfWoman &lt; (*village2)-&gt;countOfWoman) {
        result = -1;
    }
    else if ((*village1)-&gt;countOfWoman &gt; (*village2)-&gt;countOfWoman) {
        result = 1;
    }
    return result;
}

Furthermore, the arguments of the sorting function needs to generic constant pointers (const void *) to be really correct, and then you should cast them to the correct pointer type inside the function.

huangapple
  • 本文由 发表于 2020年1月3日 23:12:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/59580968.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定