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, 01:44 PM
Registered User
 
Join Date: Mar 2008
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default List not sorting, round two

Ok, dictionaries are beyond me at the moment, so I went a slightly different direction.
(PS, this is running as a console app, not a win app)
I'm creating a list of classes, and want to sort on the third value of the class, so that if the line that is read in from the CSV file is:

34919, 12, 0444, 20, Some Text

I want to sort on 0444.

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

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

Also posted to the C# 2008, I forgot that's the developer version I am running as opposed to the book I'm using. :/

John





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
Sorting a list/dictionary? Devlin C# 2005 6 March 12th, 2008 12:34 AM
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
Datagrid sorting by non alphabetical sorting? LLAndy VS.NET 2002/2003 1 July 15th, 2004 01:20 AM





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