1

I'm implementing the 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.

How could I sort the contents by filename?

3
  • 1
    Possible duplicate of How to sort files in some directory by the names on Linux using C - stackoverflow.com/questions/5102863/… Commented Oct 20, 2011 at 2:45
  • Looks like homework -- if so, please tag it as such. Commented Oct 20, 2011 at 2: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? Commented Oct 20, 2011 at 2:47

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@Emil: Yep. The system wants to make sure there's time for other answers to be posted, so you can accept the best one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.