I have a text file with deliveries (id,weight,...etc,) and I read them from a Text file and save them in my list. After my list is ready I have to sort it in correct order and only then I can work.
My question: Is there a way to get list sorted while reading the data from the text file, so that each next deliverable that is being read from the file, must be inserted in the list immediately at the right place.
You can see my LoadDeliverablesFromFile
method below :
public void LoadDeliverablesFromFile(String filename)
{
StreamReader sr = null;
string s;
try
{
sr = new StreamReader(new FileStream(filename, FileMode.Open, FileAccess.Read));
this.myDeliverables.Clear();
s = sr.ReadLine();
while (s != null)
{
string[] words = s.Split(' ');
int id = Convert.ToInt32(words[0]);
int weight = Convert.ToInt32(words[1]);
int buyersID = Convert.ToInt32(words[2]);
Deliverable del = new Deliverable(id, weight, FindPerson(buyersID));
myDeliverables.Add(del);
s = sr.ReadLine();
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
finally
{
if (sr != null) sr.Close();
}
}