使用标准c库函数对struct(ure)s数组进行排序?
问题描述:
我有一个结构数组,每个都是这种类型
struct A {
char filename [];
off_t filesize;
};
我正在尝试根据filesize对这个结构数组进行排序,以便我可以打印这个数据数据中最大的5个文件(包括文件名及其大小)。
我也试图避免手动写泡泡排序或任何算法。那么我可以获取标准c库中的任何函数吗?
I have an array of structures, each of this type
struct A {
char filename[];
off_t filesize;
};
I''m trying to sort this array of structs according to filesize, so that I can , for instance, print the 5 largest file (include filename and its size) from this array of data.
Also I''m trying to avoid manually write up bubble sort or whatever algorithm. So are there any functions from standard c libraries that I can grab?
答
C标准库中有一个名为qsort的函数可以执行您想要的操作。你现在要做的就是查看文档。
干杯!
There is a function in the C standard library called qsort that will do what you want to. All you have to do now is to look into the documentation.
Cheers!