Emil
Emil

Reputation: 565

Unix ls -l implementation - sorting by name?

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?

Upvotes: 1

Views: 417

Answers (1)

Greg Hewgill
Greg Hewgill

Reputation: 993323

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.

Upvotes: 2

Related Questions