我想按字母顺序对数组进行排序,如下所示:
我尝试了各种方法,但仍然没有办法。它崩溃了,还不知道为什么。你有一些提示可以解决这个问题吗?
由于我不是那么专家,因此代码旨在简单(阅读/理解)并完成工作。
谢谢,问候
/* note: datalist[...] is basically the ouput from a sort of ls (dir
read) that it unsorted. So that: datalist[0] is ".." datalist[1] is
"file2.c" datalist[2] is "file34.c" and so on.*/
char datalist[500][2024] ;
void sortData(int aoptiondt) { ///////////////////////////LOCAL
DECLARATIONS///////// int MAX = 500 ; int current; int walker;
int smallestIndex; char* temp;
///////////////////////////DEFINITIONS//////////////////
for (current = 0; current < MAX - 1; current++)
{
smallestIndex = current;
for (walker = current; walker < MAX ; walker ++)
{
if (strcmp(datalist[walker], datalist[smallestIndex]) < 0)
smallestIndex = walker;
} //for walker
//Swap to position smallest at what is the current position
strncpy( temp , datalist[current] , PATH_MAX);
strncpy( datalist[current] , datalist[smallestIndex] , PATH_MAX);
strncpy( datalist[smallestIndex] , temp, PATH_MAX);
} //for current }
return; }
//blabla
int main() {
}