• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sort files by date

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I have the following code to recursively search for all files in a directory and it's subfolders.
It appears to be working fine. How do I display the results by last modified date (oldest first)?


 
Marshal
Posts: 8863
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Let's not dive straight to implementation details. Let's think for a moment, what you need to do differently from what you are doing now in order to be able to define which file is most recent which one is most old?
 
Nasio denasio
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:Welcome to the Ranch.

Let's not dive straight to implementation details. Let's think for a moment, what you need to do differently from what you are doing now in order to be able to define which file is most recent which one is most old?



This statement is ridiculous considering that this is a Beginning Java forum. Do you tell everyone who asks question here to think for a moment what they need to do differently to make the code work? These forums are for asking questions or not?

If you want to help with tips or code, then that's great! Otherwise keep quiet and let others who are willing to help answer.
 
Liutauras Vilda
Marshal
Posts: 8863
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nasio denasio wrote:Do you tell everyone who asks question here to think for a moment


No.

Nasio denasio wrote:These forums are for asking questions or not?


Yes. I really believed I did.

Nasio denasio wrote:If you want to help with tips or code


Really wanted.

Let's hope somebody else can join and help you. I apologise if I offended you somehow. I rather go back to work then.
 
Saloon Keeper
Posts: 10732
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nasio denasio wrote:

First, I assume you want to sort all files together, not sort the files locally under each directory. (Correct me if I'm wrong) So, in order to sort them you'll need to gather them up into an array or a List. Arrays in recursion are very difficult to manage. Lists are far more easier as they can expand dynamically as needed. This means that your method listFiles() will need to add all found files to a growing list. Once the recursion is complete and the list returns, then it's a relatively simple matter to sort the list. I'm going to assume that you've covered List and ArrayList is your studies.

Before adding a list to your listFiles() method I would highly recommend a minor enhancement: don't pass around directory names as Strings, pass them around as a File type. It's much more efficient to keep things as a 'File' rather than converting back and forth between 'File' and 'String' (e.g. your call to getAbsolutePath()).

With the change your method would start out
and your recursive call would be
and your call in main() would be
Let's start with this much before going on to creating a List.
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nasio denasio wrote:
This statement is ridiculous considering that this is a Beginning Java forum. Do you tell everyone who asks question here to think for a moment what they need to do differently to make the code work? These forums are for asking questions or not?

If you want to help with tips or code, then that's great! Otherwise keep quiet and let others who are willing to help answer.


This is not an appropriate way to speak on this forum (or anywhere, I would hope).  Liutauras is a seasoned admin on this forum and he helps many people for free.  There is no need to berate him, even if you think his post is not useful.

In the Beginning Java forum it is very common for people to tell posters to think about their programs and not start coding right away.  We tend to guide the poster to their own conclusion.  I can vouch that the way Liutauras responded is in no way unique to you.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:I can vouch that the way Liutauras responded is in no way unique to you.


And I can vouch that in no way whatsoever was Liutauras' response meant to be offensive, condescending, or anything other than helpful and well-intentioned.
 
Nasio denasio
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:This is not an appropriate way to speak on this forum (or anywhere, I would hope).  Liutauras is a seasoned admin on this forum and he helps many people for free.  There is no need to berate him, even if you think his post is not useful.

In the Beginning Java forum it is very common for people to tell posters to think about their programs and not start coding right away.  We tend to guide the poster to their own conclusion.  I can vouch that the way Liutauras responded is in no way unique to you.



I do not agree with you. Liutauras may be a seasoned admin, but that does not mean that he is right in this case.
When you have a Forum targeted for Beginners, you have to expect question from these beginners that for you may seem stupid, easy or even trivial.
A beginner knows much less than an experienced programmer, so what I expect from such a Beginners forum is people that have patience and really "help" the beginners to solve their problem.
The description of this forum is as follows: We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.
This "help" can come in several ways: tips, hints, code, link to solutions, advice, java code, etc. All these will help the beginner to understand the language and understand how to solve the problem at hand.
But you do NOT tell a member that is looking for help, especially in the Beginners forum, to think about their programs before coding!!! You call that help? That is not helping! That's an insult!
Don't you think I have thought about it, couldn't figure it out, and then posted the question here for help?
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Like the title of your post implies: Sort the files by date first, then print them.
 
Rancher
Posts: 517
15
Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

How do I display the results by last modified date (oldest first)?



Assuming that you already have the files collected in a List or an array, Java SE has API's to sort a List or an array.

Optionally, there are other libraries which can be used for the same functionality (sorting), often more efficiently. Apche Commons IOis such a library of utilities to work with IO functionality. org.apache.commons.io.comparator package in that library provides various Comparator (java.util.Comparator) implementations for Files. LastModifiedFileComparator is one of the classes. This class can be used to sort lists or arrays of files (java.io.File objects) by their last modified date/time.  

If you have an unsorted List of File objects (lets call it fileListUnsorted) this is how it can be sorted by last modified date/time:

 
Ranch Hand
Posts: 235
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nasio denasio wrote:
This statement is ridiculous considering that this is a Beginning Java forum. Do you tell everyone who asks question here to think for a moment what they need to do differently to make the code work? These forums are for asking questions or not?

If you want to help with tips or code, then that's great! Otherwise keep quiet and let others who are willing to help answer.



I found your reply rather irksome. so instead of trying to explain anything to you, I will just point you (not that I think you will actually read it) to the We Are Not a Code MIll documentation.  [edit: comment deleted (none of my affair_]
 
Paul Clapham
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think that nasio has asked for us to write code for them, and it's unfair to imply that. Their request for "tips or code" is just fine.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nasio denasio wrote:
But you do NOT tell a member that is looking for help, especially in the Beginners forum, to think about their programs before coding!!! You call that help? That is not helping! That's an insult!
Don't you think I have thought about it, couldn't figure it out, and then posted the question here for help?


Again, you haven't been singled out here nor was there any intent to insult you. That's simply your interpretation of what he wrote.

We often say that kind of thing to programmers, both new and experienced, to get them to back off from thinking about implementation and think about intent instead. This is critical skill you need to learn as a programmer. Thinking about intent can often open your mind up to see other possible ways to write the implementation. It can also help you see how to separate parts of the program to different methods so that you are only tackling one part of the program at a time.

I think Liutauras was trying to get you to think about how to break your problem down into separate smaller tasks like maybe creating a list of files first in one method, then use another method to sort the files by modified date in descending ascending order.

One technique to writing better, cleaner code is to Compose Method.  For example, if I were to use that technique, I would first write:


This is the kind of code I get by "not diving straight into the implementation" -- I wrote code that explains my intent first. When I have this clearly understood in my mind, then I can start going into each method and start thinking more at an implementation level perspective.

Does this help?
 
Prasad Saya
Rancher
Posts: 517
15
Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Optionally, one can put together some pseudo-code first, (adding to what Junilu Lacar had mentioned about intent). The intent actually shows what is to be done, rather how it is done:

  • Get the list of files from the directory
  • Sort the files, and
  • Print them


  • That is the requirement for this application - sort and print all the files in a directory

    Then there are various APIs, libraries and Java versions, to choose from, that provide different ways to write the code. For example, Java SE 7 and 8 has java.nio.file.Files.walkFileTree and walk API methods respectively to consider.
     
    Liutauras Vilda
    Marshal
    Posts: 8863
    637
    Mac OS X VI Editor BSD Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I'm quite surprised all this grown that high.

    Nasio denasio wrote:When you have a Forum targeted for Beginners, you have to expect question from these beginners that for you may seem stupid, easy or even trivial.


    Just to be clear on that. I did not think your question seemed or was stupid. I had absolutely no intention, nor have now to think that way. Fairly recently I was same beginner as you, so I went through all that, and your question seemed to me kind of the same as I had at some point. Most of what I know now I've learned partially from books, university, and the bigger portion of that from these forums here at CodeRanch.

    Nasio denasio wrote:The description of this forum is as follows: We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.


    That's exactly the pick up line I was trying to follow. Rather than trying to explain exactly what you need to do, I thought I'll try to guide you first through possible thinking process, so you could try to understand the problem you have at hand and how it possibly could be tackled. Carey Brown in his very first reply basically took the words from my mouth what I had in mind, but did not write, so wanted that you'd come up with such conclusion. I have mind this:

    Carey Brown wrote:...in order to sort them you'll need to gather them up into an array or a List.


    So, what I wrote:

    Liutauras Vilda wrote:...what you need to do differently from what you are doing now in order to be able to define which file is most recent which one is most old?


    As you see, I wanted to guide you (I bolded the part which I thought might work as a hint), that instead of printing all files in sequential way and throwing them out, you possibly need to gather them first so you could sort them among each other. Ok, one can argue, I might was not that clear, but hey, our community is big, if one is less than optimum with his answer - others step in.

    Nasio denasio wrote:But you do NOT tell a member that is looking for help, especially in the Beginners forum, to think about their programs before coding!!!


    Again, Junilu mentioned that already. You somehow misinterpreted my tone (possibly because we are not discussing in person). I did not tell, I just asked, and this asking wasn't directed to your person only. I kind a had in mind, "alright..., let's (let us) think first, what we need...?", not sure again if the actual sounding is easy to express in text and is understandable. But I did not have in mind: "You need to think first!" <-- with a hidden implication that you're not thinking.

    Nasio denasio wrote:Don't you think I have thought about it, couldn't figure it out, and then posted the question here for help?


    And that is fine, that is why we are all here hobbying and trying to help. Not telling, just asking, discussing, thinking together, learning from each other, sometimes even making jokes from each other (in a friendly manner). And of course we aren't robots. Sometimes we have worse days than others (not just moderators, I'm referring to all community members) - and that is understandable. I'm not separating moderators/non moderators - it is a community and each member is equally important.

    Not sure if that helps you feel somehow better (in case you don't), but I do not have any anger, so you can assume it never happened, nor I think actually something big happened. Some people having much bigger problems than that

    So let's get back on the track and crack this problem out. Junilu gave some good tips (I learned a lot from him too) how to divide and conquer the problem. These method headers supposed to give a pretty good idea what needs to be done further tackling this problem.

     
    Nasio denasio
    Greenhorn
    Posts: 13
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hello,

    Thanks for the tips. Ok, so I have to change my approach. From what you guys are suggesting the way to do this is:

    1) First recursively get the list of files from the directories
    2) Sort the files
    3) Then print the files

    This seems to be the right solution.

    While thinking of this solution, another solution crossed my mind:

    1) Recursively get the list of files from the directories and use them to populate a JTable
    2) Make the JTable sortable, this way I can sort by name, last modified, size, etc. by clicking the column headers





     
    Paul Clapham
    Marshal
    Posts: 28226
    95
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Sure, you could use a JTable to sort the files. That way you could sort based on things other than the last-changed-date. And if that fits with your original requirements, it would be fine.

    But if your requirement was to select the files, sort them by date, and write the sorted list to a text file, then inserting a JTable and having a person click on a column heading would be an unnecessary diversion, I think.

    So it all depends on the requirements and their background.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic