0

how to sort the group of files in the directory using Hashtable by values?

i'll have more than 500 no of files in the below format:

prod_orders_XXX_<TimeStamp>.dat

XXX = symbol of the product and the length may varies.
<TimeStamp> = date and time

Multiple files for the same XXX are possible with different time stamps.

Here are some examples:

prod_orders_abc_20122001083000.dat
prod_orders_abc_20122001083111.dat
prod_orders_xyz_20122001093157.dat
prod_orders_xyz_20122001083000.dat
prod_orders_abc_20122001163139.dat
prod_orders_abc_20122001093137.dat

I have posted a similar question before but this time i need this specificly using Hashtable. Can someone help ?

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Santhosh_ms3
  • 110
  • 2
  • 3
  • 15

1 Answers1

1

You have four problems here.

  1. You shouldn't use an untyped hashtable at all. A generic Dictionary<K,V> is a much better option.
  2. You did not share how you will determine the key for each file name. Items in a hashtable must have both a key and a value. Presumably the file names are the value, but we have no information on the key.
  3. You did not specify what criteria will be used to determine the sort order. Sort by timestamp? File name? Product symbol? With what precedence?
  4. Hashtables are Dictionaries are unsorted by definition. There is no way to sort them. Period. End of story. You can iterate over their contents in a sorted way, but you cannot force it to store sorted items, and attempting to do so would defeat the nice performance benefits of these collections.
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • Actually i want to sort the filenames based on the timestamp mentioned in the file. i thought that timestamp can be the key and the string part can be the key value. finally we can assign the keys into arraylist and then sort the array and can manipulate the rest of filename. but i'm facing problems when two files with different product and with the same time stamp. i have posted this scenario as new question. [link] (http://stackoverflow.com/questions/9033092/filesort-in-visual-basic-based-timestamp-mentioned-in-the-file-names) – Santhosh_ms3 Jan 27 '12 at 14:22
  • There you go with the untyped collection again. Unless you're stuck on .Net 1.1, there's no good reason you should **ever** use ArrayList again. List is pretty much always preferred. – Joel Coehoorn Jan 27 '12 at 14:28
  • i'm just trying out possible ways to get it efficiently. Thanks for your responses and appologies for the inadequate informations. – Santhosh_ms3 Jan 27 '12 at 14:35