By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,665 Members | 1,367 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,665 IT Pros & Developers. It's quick & easy.

How to sort folders according to creation time

P: 2
I need to sort around 17000 folders, in according to the last modified date ( creation time is also fine with me).

Here is my code
Expand|Select|Wrap|Line Numbers
  1.             string[] DirectoryList = Directory.GetDirectories(myPath);
  2.             ArrayList aList = new ArrayList(DirectoryList);
  3.             string temp;
  4.  
  5.  
  6.             for (int i = 0; i < aList.Count - 1; i++)
  7.             {
  8.                 Console.WriteLine("Inside first for " + i);
  9.                 for (int j = i + 1; j < aList.Count; j++)
  10.                 {
  11.  
  12.  
  13.                     DirectoryInfo dInfo = new DirectoryInfo(aList[i].ToString());
  14.                     DirectoryInfo dInfo1 = new DirectoryInfo(aList[j].ToString());
  15.                     DateTime T1 = dInfo.CreationTime;
  16.                     DateTime T2 = dInfo1.CreationTime;
  17.                     if (T1 > T2)
  18.                     {
  19.  
  20.                         temp = aList[i].ToString();
  21.                         aList[i] = aList[j].ToString();
  22.                         aList[j] = temp;
  23.                     }
  24.  
  25.  
  26.                 }
  27.  
  28.  
  29.             }
But this is very time consuming and takes hours to get completed.
Also, at times it gets stuck sumwhere, i can't figure out.

How can I optimise this code?

Or is there any other way by which I can sort the folders?

PLease help.
Jul 8 '08 #1
Share this Question
Share on Google+
6 Replies


Expert 100+
P: 427
You are doing a bubble sort, this is the slowest of all sorting methods, there are many faster algorithms out there.

Here is a link to some of the most popular algorithms:

http://www.publicjoe.f9.co.uk/csharp/sort00.html

good luck
Jul 8 '08 #2

Plater
Expert 5K+
P: 7,872
Have you tried implementing the iCompare or iComparable interface?
Jul 8 '08 #3

P: 2
I tried this...but it dint help that much
Expand|Select|Wrap|Line Numbers
  1.             string[] dirList = Directory.GetDirectories(myPath);
  2.             List<string> dirEntries = new List<string>();
  3.             foreach (string dirName in dirList)
  4.             {
  5.                 dirEntries.Add(dirName);
  6.             }
  7.             dirEntries.Sort(CompareByCreationTime);
  8.             ArrayList aList = new ArrayList(dirEntries);
  9.  
  10.  
  11.             return aList;
  12.  
  13.  
  14.  
  15. static int CompareByCreationTime(string dirOne, string dirTwo)
  16.         {
  17.             try
  18.             {
  19.                 DateTime datetimeFirst = Directory.GetCreationTime(dirOne);
  20.                 DateTime dateTimeSecond = Directory.GetCreationTime(dirTwo);
  21.                 TimeSpan resultTimeSpan = datetimeFirst - dateTimeSecond;
  22.                 return resultTimeSpan.Milliseconds;
  23.             }
  24.             catch (IOException ex)
  25.             {
  26.                 Console.WriteLine("CompareByCreationTime -- " + ex.Message);
  27.                 return 0;
  28.             }
  29.         }
PLesae help
Aug 14 '08 #4

DrBunchman
Expert 100+
P: 979
Hi ananya choudhury,

Welcome to Bytes, I hope you find a solution to your problem here.

Please could you remember to wrap your code in CODE tags using the # button at the top of the editor window - it makes your posts much easier for everybody to read. Check out the Posting Guidelines for more info about site rules.

Regards,

Dr B
Aug 14 '08 #5

Plater
Expert 5K+
P: 7,872
That is not how the camparison procsess works.
I already suggested to you what you should look up.
I recomend you take another look at MSDN and see how sort helper functions are supposed to be.
Aug 14 '08 #6

joedeene
100+
P: 583
how about sorting them on another thread using a try and catch block ?

edit: also make the thread background so it doesnt freeze

Expand|Select|Wrap|Line Numbers
  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2.         Dim mythread As Threading.Thread = New Threading.Thread(AddressOf sortfolders)
  3.         mythread.IsBackground = True
  4.         mythread.Start()
  5.  
  6.  
  7.     End Sub
  8.  
  9.     Private Sub sortfolders()
  10.         'sorting code here
  11.     End Sub
Aug 14 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.