Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 2008 > C# 2008 aka C# 3.0
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C# 2008 aka C# 3.0 Discuss the Visual C# 2008 (aka C# 3.0) language
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2008 aka C# 3.0 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old March 11th, 2008, 01:57 PM
Registered User
 
Join Date: Mar 2008
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Sorting a list by the third element

Ok, I have this posted in the C# 2005, but I realized that the studio I'm using is the newest, while the book is 2005 :D

This is running as a console app, not a win app.
I'm reading in a CSV file containing 5 elements per line into a list of objects, and I want to sort on the third element of the line. So, if the CSV line is this:

34919, 12, 0444, 20, Some Text

I want to sort on 0444. Currently it's not doing it.

Here's my code:
Code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace prgOne
{
    public class csvLineComparer : IComparer
    {
        public static IComparer Default = new csvLineComparer();
        public int Compare(object x, object y)
        {
            if (x is csvLine && y is csvLine)
            {
                return Comparer.Default.Compare(((csvLine)x).clNumb, ((csvLine)y).clNumb);
            }
            else
            {
                throw new ArgumentException("One or both objects to compare are not csvLine objects.");
            }
        }
    }
}
Code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace prgOne
{
    class csvLine : IComparable
    {
        public string clTime;
        public string clChan;
        public int clNumb;
        public string clStat;
        public string clDesc;

        public csvLine(string cltime, string clchan, int clnumb, string clstat, string cldesc)
        {
            clTime = cltime;
            clChan = clchan;
            clNumb = clnumb;
            clStat = clstat;
            clDesc = cldesc;
        }

        public int CompareTo(object obj)
        {
            if (obj is csvLine)
            {
                csvLine otherLine = obj as csvLine;
                return this.clNumb - otherLine.clNumb;
            }
            else
            {
                throw new ArgumentException("Object to compare is not a line.");
            }
        }
    }
}
Code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace prgOne
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] strArray;
            string strLine;
            char[] charArray = new char[] { ',' };

            FileStream aFile = new FileStream("C:\\csharp\\diag.log", FileMode.Open);
            StreamReader sr = new StreamReader(aFile);
            ArrayList lines = new ArrayList();

            strLine = sr.ReadLine();
            while (strLine != null)
            {
                strArray = strLine.Split(charArray);
                lines.Add(new csvLine(strArray[0], strArray[1], Convert.ToInt16(strArray[2]), strArray[3], strArray[4]));

                strLine = sr.ReadLine();
            }

            sr.Close();

            for (int i = 0; i < lines.Count; i++)
            {
                Console.WriteLine("{0}, {1}, {2}, {3}, {4}", (lines[i] as csvLine).clTime, (lines[i] as csvLine).clChan, (lines[i] as csvLine).clNumb, (lines[i] as csvLine).clStat, (lines[i] as csvLine).clDesc);
            }

            Console.ReadKey();
        }

    }
}
Reply With Quote
  #2 (permalink)  
Old March 11th, 2008, 02:06 PM
samjudson's Avatar
Friend of Wrox
Points: 8,436, Level: 39
Points: 8,436, Level: 39 Points: 8,436, Level: 39 Points: 8,436, Level: 39
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,084
Thanks: 1
Thanked 180 Times in 179 Posts
Default

a) If you're going to post two forums, or rather 'move' forums, then try to tell the people on the other forum thats what you're doing.

b) You're not sorting your list...

lines.Sort();

c) Instead of using ArrayList(), just use List<csvLine>()

/- Sam Judson : Wrox Technical Editor -/
Reply With Quote
  #3 (permalink)  
Old March 11th, 2008, 02:50 PM
Registered User
 
Join Date: Mar 2008
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by samjudson
 a) If you're going to post two forums, or rather 'move' forums, then try to tell the people on the other forum thats what you're doing.

b) You're not sorting your list...

lines.Sort();

c) Instead of using ArrayList(), just use List<csvLine>()

/- Sam Judson : Wrox Technical Editor -/
A - Got it. Apologies.
B - I must have deleted it when I was editing somewhere, it was in there originally.
C - List lines = new List<csvLines> ?

John

Reply With Quote
  #4 (permalink)  
Old March 11th, 2008, 02:54 PM
Registered User
 
Join Date: Mar 2008
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hokay, it's sorting. Is it possible to sort on two or more elements?

John

Reply With Quote
  #5 (permalink)  
Old March 12th, 2008, 02:07 AM
samjudson's Avatar
Friend of Wrox
Points: 8,436, Level: 39
Points: 8,436, Level: 39 Points: 8,436, Level: 39 Points: 8,436, Level: 39
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,084
Thanks: 1
Thanked 180 Times in 179 Posts
Default

Code:
List<csvLine> lines = new List<csvLine>();
The you can just do:

Code:
foreach(csvLine line in lines)
{
   Console.WriteLine(line.clNumb);
}
The Compare function returns 0 if the two items are the same, so simply check that value and if they are then move to the second item you want ot sort by:

Code:
public int Compare(object x, object y)
{
  csvLine l1 = x as csvLine;
  csvLine l2 = y as csvLine;
  if (l1 != null && l2 != null)
  {
    int c = Comparer.Default.Compare(l1.clNumb, l2.clNumb);
    if( c != 0 )
      return c;
    return Comparer.Default.Compare(l1.clChan, l2.clChan);
  }
  else
  {
    throw new ArgumentException("One or both objects to compare are not csvLine objects.");
  }
}
/- Sam Judson : Wrox Technical Editor -/
Reply With Quote
  #6 (permalink)  
Old March 12th, 2008, 10:47 AM
Registered User
 
Join Date: Mar 2008
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you. That helps immensely.

John

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting a list/dictionary? Devlin C# 2005 6 March 12th, 2008 12:34 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 10:50 AM
Sorting XML in a dropdown list Hughesie78 ADO.NET 1 November 23rd, 2007 07:09 AM
Sorting element in xsl Sakubar Sathik XSLT 5 April 6th, 2006 04:23 AM



All times are GMT -4. The time now is 04:59 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.