1

I'm implementing mylsl.c (which is equivalent to "ls -l") method for Unix. Everything is set pretty much, except the contents of the current directory is being shown in a random order.

So the question is how could I sort the contents by filename?

RHT
  • 4,974
  • 3
  • 26
  • 32
Emil
  • 555
  • 3
  • 7
  • 16
  • 1
    Possible duplicate of How to sort files in some directory by the names on Linux using C - http://stackoverflow.com/questions/5102863/how-to-sort-files-in-some-directory-by-the-names-on-linux – RHT Oct 20 '11 at 02:45
  • Looks like homework -- if so, please tag it as such. – Rob Oct 20 '11 at 02:45
  • @Tim sorry, I guess maybe it's because I'm not really familiar with the system.. How should I do that? (I mean there're several options like "Was it helpful?" and etc. Which one is it? – Emil Oct 20 '11 at 02:47

1 Answers1

2

The general solution to this problem is:

  • Read all the directory entries (into memory)
  • Sort them
  • Write them out in sorted order

The OS does not provide a way to list them in sorted order for you.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • @Emil: Yep. The system wants to make sure there's time for other answers to be posted, so you can accept the best one. – Rob Oct 20 '11 at 02:50