Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
| Search | Today's Posts | Mark Forums Read
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
 
Old March 11th, 2008, 11:28 AM
Registered User
 
Join Date: Mar 2008
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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

 
Old March 11th, 2008, 11:43 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

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 -/
 
Old March 11th, 2008, 11:57 AM
Registered User
 
Join Date: Mar 2008
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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();
        }
    }
}
 
Old March 11th, 2008, 12:09 PM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

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 -/
 
Old March 11th, 2008, 12:12 PM
Registered User
 
Join Date: Mar 2008
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

So to sort by the third column, I'd reference it as 'C'? Maybe?

I'll give it a try, thank you!

John

 
Old March 12th, 2008, 12:32 AM
Registered User
 
Join Date: Mar 2008
Location: Baroda, Gujarat, India.
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

 
Old March 12th, 2008, 12:34 AM
Registered User
 
Join Date: Mar 2008
Location: Baroda, Gujarat, India.
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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); });





Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting a list by the third element Devlin C# 2008 aka C# 3.0 5 March 12th, 2008 10:47 AM
List not sorting, round two Devlin C# 2005 1 March 11th, 2008 02:54 PM
Sorting XML data in a dropdown list on asp.net Hughesie78 XML 4 November 27th, 2007 11:50 AM
Sorting XML in a dropdown list Hughesie78 ADO.NET 1 November 23rd, 2007 08:09 AM





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