Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 28th, 2007, 07:42 AM
Authorized User
 
Join Date: Oct 2003
Posts: 89
Thanks: 0
Thanked 0 Times in 0 Posts
Default ArrayList sort

Hi,



A few days ago I asked how to sort files in a folder according to name and date. I need to upload the files into the database according to name and date.

I received a reply that pointed me in the right direction.

http://forums.asp.net/p/1145406/1863023.aspx#1863023



In line 215 it says that i need to sort the dates in the order that i want. However, i tried and was unable to.

First employees, then venues, seminars, cvs,SemReg,CandidteAttds,Offers.

But i also have to pay attention to the date in the file name so in the example i will first upload:

081706_ETS_Employees.xls

081507_ETS_Employees.xls
081507_ETS_Venues.xls
081507_ETS_Seminars.xls
082207_ETS_SemReg.xls
082207_ETS_CandidteAttds.xls
082207_ETS_Offers.xls

081508_ETS_Employees.xls



The files in the folder are in the format:

081507_ETS_Employees.xls
081507_ETS_Venues.xls
081507_ETS_Seminars.xls
081508_ETS_Employees.xls
081706_ETS_Employees.xls

082207_ETS_SemReg.xls
082207_ETS_CandidteAttds.xls
082207_ETS_Offers.xls



How can i sort the files correctly?



Thanks



1 private static ArrayList alFiles = new ArrayList();
2 public static string FTPconnect = ConfigurationSettings.AppSettings["FTP"].ToString();
3 public static string FTPMoveFiles = ConfigurationSettings.AppSettings["FTPMoveFiles"].ToString();
4
5 [STAThread]
6
7
8 static void Main(string[] args)
9 {
10 Application.EnableVisualStyles();
11 Application.SetCompatibleTextRenderingDefault(fals e);
12 // Application.Run(new Form1());
13 string[] myArray = new string[7];
14
15 string PartFileName = string.Empty;
16 string FilePath = string.Empty;
17 string _strSheetName = string.Empty;
18 ArrayList alLogSuccess = new ArrayList();
19 ArrayList alLogErrors = new ArrayList();
20 string cv = string.Empty;
21 string attd = string.Empty;
22 string reg = string.Empty;
23 string offers = string.Empty;
24 string Employees = string.Empty;
25 string Seminars = string.Empty;
26 string Venues = string.Empty;
27
28 excelReader exr = new excelReader();
29
30 string Location = FTPconnect; //@"C:\excelTest\";
31
32 string partFname = string.Empty;
33 System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(Location);
34 int intFiles = dir.GetFiles("*.*").GetUpperBound(0) + 1;
35 // string[] myArray = new string[intFiles];
36 string inpBodyWords = string.Empty;
37 string BodyWords = string.Empty;
38 string fromMail = string.Empty;
39 string textCV = string.Empty;
40 string AttachHtmFile = string.Empty;
41 //Initialize the name part of file. Venues is first, Seminar is second, Employees is third.
42 string[] arrayName = new string[] { "Employees","Venues", "Seminars","CVs","SemReg","CandidteAttds","Off ers" };
43
44 //First get all the files.
45 ArrayList listDate = FilesArray();
46 //Sort the date part of files.
47 listDate = FilesSort(listDate);
48
49
50 string fullpath = string.Empty;
51
52
53 if (intFiles == 0)
54 {
55
56 Console.WriteLine("no files in directory");
57 DB.noFiles("No files in directory");
58 }
59 else
60 {
61 for (int i = 0; i < listDate.Count; i++)
62 {
63 for (int j = 0; j < 7; j++)
64 {
65 //Get complete name of file
66 string strFile = listDate[i].ToString() + "_ETS_" + arrayName[j] + ".xls";
67 fullpath = FTPconnect + strFile;
68
69
70 //
71 //TODO: Import this file into database
72
73 if (File.Exists(fullpath.ToString()))
74 {
75 //File.Create(@"c:/files.txt");
76 File.AppendAllText(@"c:/files.txt",fullpath.ToString());
77 string UsedLocation = FTPMoveFiles;//@"C:\excelTest\Old\";
78 FilePath = fullpath.ToString();
79 // CallReader(FilePath, _strSheetName, fullpath.ToString());
80 if (File.Exists(UsedLocation + fullpath.ToString()))
81 {
82 Console.Write("File exists in location");
83 }
84 else
85 {
86 File.Move(FilePath, UsedLocation + strFile.ToString());
87 // myArray[i] = null;
88 }
89 }
90 else
91 {
92 // File.Move(FilePath, UsedLocation + myArray[i].ToString());
93 Console.Write("File does not exist in location");
94 if (fullpath.Contains ("Employees"))
95 {
96
97 DB.noFiles("No employees file for the " + " " + listDate[i].ToString() + " in directory");
98 }
99
100 if (fullpath.Contains("Venues"))
101 {
102
103 DB.noFiles("No Venues file for the " + " " + listDate[i].ToString() + " in directory");
104 }
105
106 if (fullpath.Contains("Seminars"))
107 {
108
109 DB.noFiles("No Seminars file for the " + " " + listDate[i].ToString() + " directory");
110 }
111
112 if (fullpath.Contains ("CVs"))
113 {
114
115 DB.noFiles("No CVs file for the " + " " + listDate[i].ToString() + " directory");
116 }
117 if (fullpath.Contains("SemReg"))
118 {
119
120 DB.noFiles("No SemReg file for the " + " " + listDate[i].ToString() + " in directory");
121 }
122 if (fullpath.Contains("CandidteAttds"))
123 {
124
125 DB.noFiles("No CandidateAttended file for the " + " " + listDate[i].ToString() + " in directory");
126 }
127
128 if (fullpath.Contains("Offers"))
129 {
130
131 DB.noFiles("No Offers file for the " + " " + listDate[i].ToString() + " in directory");
132 }
133 //end of check if files exist
134
135 }
136
137 // }
138 //
139 }
140
141 }
142 }//end else intfiles
143
144 }
145
146 /// <summary>
147 /// call db functions
148 /// </summary>
149 /// <returns>The ArrayList includes the name of Excel</returns>
150 private static void CallReader(string FilePath, string _strSheetName, string partFname)
151 {
152 //System.IO.FileInfo f;
153 excelReader exr = new excelReader();
154 DataTable _dt;
155 exr.ReadExcel(FilePath, out _strSheetName);
156 _dt = exr.GetTable(FilePath, _strSheetName, partFname);
157
158 }
159 /// <summary>
160 /// Get all the files
161 /// </summary>
162 /// <returns>The ArrayList includes the name of Excel</returns>
163 private static ArrayList FilesArray()
164 {
165
166 ArrayList list = new ArrayList();
167 string Location = FTPconnect;
168 System.IO.DirectoryInfo dirInfo = new System.IO.DirectoryInfo(Location);
169 FileInfo[] fileInfoArray = dirInfo.GetFiles("*.xls");
170
171 foreach (System.IO.FileInfo f in dirInfo.GetFiles("*.xls"))
172 {
173
174 list.Add((f.Name));
175 }
176
177 return list;
178
179 }
180
181 /// <summary>
182 /// Sort the file name according to the requirement.
183 /// </summary>
184 /// <param name="list">The ArrayList includes the name of Excel</param>
185 /// <returns>The Date port of Arraylist be sorted</returns>
186 public static ArrayList FilesSort(ArrayList list)
187 {
188 ArrayList listDate = new ArrayList();
189
190 //for (int i = 0; i < list.Count; i++)
191 foreach (string str in list)
192 {
193 //Get Date part
194 string strDate = str.ToString().Substring(0, 6);
195
196 //You should check the date to see if the date has been in the listDate.
197 // If the date has been in the listDate, you should not add it to listDate.
198 //
199 //TODO: Check date
200 //
201
202 if (listDate.Contains(strDate) )
203 {
204 Console.Write("date in list already");
205 }
206 else
207 {
208 listDate.Add(strDate);
209
210 }
211
212 }
213
214 //
215 //TODO: Sort the Date by your requirement.Please //make sure there is the unique date in the listDate.
216 //
217
218 return listDate;
219 }
220 }


 
Old August 28th, 2007, 08:08 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You are asking for some custom sorting, not something normally achievable using numeric or alphabetic sorting.

One good way of doing this would be to create a class that implements the IComparer Interface. The "Compare" method you implement would do the comparison based on your custom logic. Then you can sort the arraylist using your custom comparer which could also be reused in other places.

-Peter





Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic sort order or sort datatype kapy_kal XSLT 2 September 18th, 2007 02:10 PM
Arraylist::Sort() by class member jilly Visual C++ 2005 0 March 22nd, 2006 06:49 PM
how to sort cross tab.sort based on row total joxa83 Crystal Reports 7 March 2nd, 2006 09:12 AM
Unable to sort using xsl sort command sly_jimmy_boy XSLT 3 June 17th, 2005 05:15 AM
ArrayList kobystud C# 4 May 25th, 2004 02:05 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.