|
C# 2005 For discussion of Visual C# 2005. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the C# 2005 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
|
|
|
March 11th, 2008, 11:28 AM
|
Registered User
|
|
Join Date: Mar 2008
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Sorting a list/dictionary?
Hey all - Fairly new to C#. I'm working out of the book "Visual C# 2005", specifically the example for reading a CSV file on pp. 719-723.
I would like to sort the list, but I'm not sure how to construct the sort. I am reading a CSV file that has 5 elements per line, and I would like to sort by the third element.
Any suggestions/pointers?
John
|
March 11th, 2008, 11:43 AM
|
|
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
Generally you can sort a list by the value of its object, or by its key value. Some lists do not however provide sorting functionality.
If you have created a custom class to store the values of each row then you might want to create a customer class which does the comparison, such as the example in this page: http://msdn2.microsoft.com/en-us/library/aw9s5t8f(VS.80).aspx
I'm not familiar with the book however - it might be that this topic is covered a little further on. Perhaps you should look it up in the index or just keep reading till you get to it.
If you still want help then an example of the code you have got which creates you list might be helpful.
/- Sam Judson : Wrox Technical Editor -/
|
March 11th, 2008, 11:57 AM
|
Registered User
|
|
Join Date: Mar 2008
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I'm pretty much a beginner, this is an exercise that I was working on to try and get familiar with lists and dictionaries, so I still don't fully understand what it's doing with regards to the list. This is pretty much straight out of the book:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace csvReader1
{
class Program
{
private static List<Dictionary<string, string>> GetData(out List<string> columns)
{
string strLine;
string[] strArray;
char[] charArray = new char[] {','};
List<Dictionary<string, string>> data = new List<Dictionary<string, string>>();
columns = new List<string>();
try
{
FileStream aFile = new FileStream("C:\\csharp\\diag.log", FileMode.Open);
StreamReader sr = new StreamReader(aFile);
strLine = sr.ReadLine();
strArray = strLine.Split(charArray);
for (int x = 0; x <= strArray.GetUpperBound(0); x++)
{
columns.Add(strArray[x]);
}
strLine = sr.ReadLine();
while (strLine != null)
{
strArray = strLine.Split(charArray);
Dictionary<string, string> dataRow = new Dictionary<string,string>();
for (int x=0; x <= strArray.GetUpperBound(0); x++)
{
dataRow.Add(columns[x], strArray[x]);
}
data.Add(dataRow);
strLine = sr.ReadLine();
}
sr.Close();
return data;
}
catch (IOException ex)
{
Console.WriteLine("An IO exception has been thrown!");
Console.WriteLine(ex.ToString());
Console.ReadLine();
return data;
}
}
static void Main(string[] args)
{
List<string> columns;
List<Dictionary<string, string>> myData = GetData(out columns);
foreach (string column in columns)
{
Console.Write("{0, -15}", column);
}
Console.WriteLine();
foreach (Dictionary<string, string> row in myData)
{
foreach (string column in columns)
{
Console.Write("{0, -15}", row[column]);
}
Console.WriteLine();
}
Console.ReadKey();
}
}
}
|
March 11th, 2008, 12:09 PM
|
|
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
In theory you could do what you are trying to do.
Code:
int MyCompare( Dictionary<string, string> x, Dictionary<string, string> y )
{
// sort by column 'A'
return x['A'].Compare(y['A']);
}
You'd then sort you list something like this:
Code:
myData.Sort(MyCompare);
/- Sam Judson : Wrox Technical Editor -/
|
March 11th, 2008, 12:12 PM
|
Registered User
|
|
Join Date: Mar 2008
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
So to sort by the third column, I'd reference it as 'C'? Maybe?
I'll give it a try, thank you!
John
|
March 12th, 2008, 12:32 AM
|
Registered User
|
|
Join Date: Mar 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hey John,
simply use your list's sort method in which you pass the property name using which you want to sort the list...
suppose i have list named people.in which i have elements age,name,and nickname of Person class..if i want to sort the list by nickName.. i just pass it in argument.
people.Sort(delegate(Person p1, Person p2) { return p1.nickName.CompareTo(p2.nickName); });
Quote:
quote:Originally posted by Devlin
Hey all - Fairly new to C#. I'm working out of the book "Visual C# 2005", specifically the example for reading a CSV file on pp. 719-723.
I would like to sort the list, but I'm not sure how to construct the sort. I am reading a CSV file that has 5 elements per line, and I would like to sort by the third element.
Any suggestions/pointers?
John
|
|
March 12th, 2008, 12:34 AM
|
Registered User
|
|
Join Date: Mar 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hey John,
simply use your list's sort method in which you pass the property name using which you want to sort the list...
suppose i have list named people.in which i have elements age,name,and nickname of Person class..if i want to sort the list by nickName.. i just pass it in argument.
people.Sort(delegate(Person p1, Person p2) { return p1.nickName.CompareTo(p2.nickName); });
|
|
|