Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Fern Sanchez   on Nov 23 In Java Category.

  
Question Answered By: Isaac Evans   on Nov 23

I tried & it's working fine... u can use it... (change the dir name to ur dir)


import java.io.*;
import java.text.*;

public class DateSort
{
public static void main(String args[])
{
try
{
SimpleDateFormat sdfDate = new SimpleDateFormat("MMMyyyy");
File fDir = new File("C:\\Vasanth\\Java\\MyDirectory");
File files[] = fDir.listFiles();
long lDates[] = new long[files.length];

for(int i=0;i<files.length;i++)
{
lDates[i] = sdfDate.parse(files[i].getName()).getTime();
}

for(int i=0;i<lDates.length;i++)
{
for(int j=0;j<i;j++)
{
if(lDates[i] < lDates[j])
{
File fTemp = files[i];
long lTemp = lDates[i];

files[i] = files[j];
lDates[i] = lDates[j];

files[j] = fTemp;
lDates[j] = lTemp;
}
}
}

for(int i=0;i<files.length;i++)
{
System.out.println(files[i].getName());
}
}
catch(Exception ex)
{
System.out.println(ex.toString());
}
}
}

Share: