Sorting an array of directories

aewarnick

Senior Contributor
Joined
Jan 29, 2003
Messages
1,031
Array.Sort() does not work for sorting directory paths in alphebetical order. I also made this loop to sort them but it does not work either:
Code:
public static string[] Sort_DirectoryPathsByAll(string[] paths, char AorD)
			{
				if(paths.Length==1)  return paths;
				string[]Pths=new string[paths.Length];
				Array.Copy(paths, Pths, paths.Length);
				string hold="";
				
				if(AorD=='A')
				{
					for(int i=0; i < Pths.Length; i++)
					{
						for(int j=0; j < Pths.Length-1; j++)
						{
							for(int k=Pths[j].Length, l=Pths[j+1].Length; (k<0 && l<0); k--, l--)
							{
								if(Pths[j][k] > Pths[j+1][k])
								{
									hold= Pths[j+1];
									Pths[j+1]= Pths[j];
									Pths[j]= hold;
								}
							}
						}
					}
				}
				else
				{
					
				}
				return Pths;
			}

How else can I do this?
 
I just came up with a method that is much better but still does not sort right at all! I have even printed this method out and looked over it but could not find any reason why it does not work. Can someone help?

Also, how do I put C# in here instead of code?
Code:
public static string[] Sort_DirectoryPaths(string[] paths, char AorD)
			{
				if(paths.Length==1)  return paths;
				string[]Pths=new string[paths.Length];
				Array.Copy(paths, Pths, paths.Length);
				string hold="";
				
				if(AorD=='A')
				{
					for(int i=0; i < Pths.Length; i++)
					{
						for(int j=0; j < Pths.Length-1; j++)
						{
							int shortt = 0;  int longg = 0;
							if(Pths[j].Length < Pths[j+1].Length) {shortt= 0; longg= 1;}
							else if(Pths[j].Length > Pths[j+1].Length) {shortt= 1; longg= 0;}

							if(Pths[j]==Pths[j+1]) {}
							else if(Pths[j+longg].StartsWith(Pths[j+shortt]) && shortt != longg)
							{
								hold= Pths[j+1];
								Pths[j+1]= Pths[j];
								Pths[j]= hold;
							}
							else
							{
								for(int k=0; k<Pths[j].Length && k<Pths[j+1].Length; k++)
								{
									if(Pths[j][k] > Pths[j+1][k])
									{
										hold= Pths[j+1];
										Pths[j+1]= Pths[j];
										Pths[j]= hold;
										k= Pths[j].Length;
									}
								}
							}
						}
					}
				}
				else
				{
					
				}
				return Pths;
			}
 
Back
Top