次のような配列をアルファベット順に並べ替えたいと思います。
いろいろな方法を試しましたが、まだ方法がありません。クラッシュしますが、その理由はまだわかりません。この問題から始めるためのヒントはありますか?
私はそれほど専門家ではないので、コードは (読んだり理解したりするために) シンプルで仕事をすることを目的としています。
thx、よろしく
/* 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() {
}