Link to home
Start Free TrialLog in
Avatar of RTSol
RTSol

asked on

Sorting a List<Currency>

Hi,

I have a list of currencies I need to sort. How can that be done?
public class Currency
    {
        public  string lCountry{get;set;}
        public string lCurrency{get;set;}
    }

--------------------------------

        private List<Currency> getCurrency()
        {
            string textstring;
            string[] text;
            if (System.IO.File.Exists(currURI))
            {
                using (StreamReader sr = new StreamReader(currURI))
                {
                    textstring = sr.ReadToEnd();
                }
                textstring = textstring.Replace(System.Environment.NewLine, string.Empty);
                text = textstring.Split(';');
            }

            List<Currency> currlist = new List<Currency>();

            foreach (string part in text)
            {
                if (part != "")
                {
                    string[] parts = part.Split(',');
                    currlist.Add(new Currency { lCountry = parts[0].Trim(), lCurrency = parts[1].Trim() });
               }
            }

            //Sorting on lCurrency should take place here!

            return currlist;
        }

Open in new window

Avatar of CCongdon
CCongdon
Flag of United States of America image

Whatever calls this method should then be able to invoke the Sort() method inherent to List<>
Avatar of RTSol
RTSol

ASKER

The sort method needs some arguments since my list is a collection of Currencies. Can you help me with this?

currlist.Sort() doesn'r work.
All you need to do is call the List<T>.Sort() function:

See code

private List<Currency> getCurrency()
        {
            string textstring;
            string[] text;
            if (System.IO.File.Exists(currURI))
            {
                using (StreamReader sr = new StreamReader(currURI))
                {
                    textstring = sr.ReadToEnd();
                }
                textstring = textstring.Replace(System.Environment.NewLine, string.Empty);
                text = textstring.Split(';');
            }

            List<Currency> currlist = new List<Currency>();

            foreach (string part in text)
            {
                if (part != "")
                {
                    string[] parts = part.Split(',');
                    currlist.Add(new Currency { lCountry = parts[0].Trim(), lCurrency = parts[1].Trim() });
                }
            }

            //Sorting on lCurrency should take place here!
            currlist.Sort();

            return currlist;
        }

Open in new window

Sorry, Got your post after I had posted.

If .Sort() does not work then use:



currlist.Sort(new Comparison<Currency>(Currency.ToDecimal());

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RTSol

ASKER

This line throws an exeption. Is the sorting performed on lCountry or on lCurrency?

The following line produces a sorted list

var Sorted = currlist.OrderBy(p => p.lCurrency);

but this is not of the type System.Collections.Generic.List<CurrencyList> so if I use this I need to convert Sorted to System.Collections.Generic.List<CurrencyList>.

Any ideas?
Ooh, a lambda expression would mean that you can sort with LINQ...

    var Sorted = currlist.OrderBy(p => p.lCurrency).ToList();
Naughty, naughty!!  You didn't say that you were working with 3.5 and LINQ *BIG GRIN*.
Avatar of RTSol

ASKER

I am sorry guys - I feel a bit stupid. Please look at my code and tell me whats wrong.

currenList doesn't return anything

Help !!

btw the code below gave a buid error

        private class CurrencyComparer : IComparer<Currency>
        {

            public int Compare(Currency x, Currency y)
            {
                return string.Compare(x.lCurrency, y.lCurrency);
            }

        }

saying: Elements defined in a namespace cannot be explicitly declared as private, protected, or protected internal
Here is my call to the list method:

            List<Currency> clist = currenList();

Here is the method:

        private List<Currency> currenList()
        {
            List<Currency> currlist = new List<Currency>();
            if (System.IO.File.Exists(currURI))
            {
                using (StreamReader sr = new StreamReader(currURI))
                {
                    textstring = sr.ReadToEnd();
                }
                textstring = textstring.Replace(System.Environment.NewLine, string.Empty);
                text = textstring.Split(';');
            }
            foreach (string tt in text)
            {
                if (tt != "")
                {
                    string[] tts = tt.Split(',');
                    currlist.Add(new Currency { lCountry = tts[0].Trim(), lCurrency = tts[1].Trim() });
                }
            }
            var Sorted = currlist.OrderBy(p => p.lCurrency).ToList();
            return Sorted;
        }

and here is my Currency class:

    public class Currency
    {
        public  string lCountry{get;set;}
        public string lCurrency{get;set;}
    }

Open in new window

You have to define private classes in the right place inside the braces:

namespace TestApplication
{

      public class TestClass
      {
           
             private class MyClass
             {
             }

      }
}

I would say that if the method is not returning anything that you need to step through the code, and see if it is calling this line:

                  currlist.Add(new Currency { lCountry = tts[0].Trim(), lCurrency = tts[1].Trim() });
 
Try This:

private List<Currency> currenList(string currURI)
        {
            var strings = new string[0];
            if (File.Exists(currURI))
            {
                using (var sr = new StreamReader(currURI))
                {
                    strings = sr.ReadToEnd().Replace(Environment.NewLine, string.Empty).Split(';');
                }
            }

            if (strings.Length.Equals(0)) return new List<Currency>();

            var currlist = (from tt in strings
                            where tt != ""
                            select tt.Split(',')
                            into tts select new Currency {lCountry = tts[0].Trim(), lCurrency = tts[1].Trim()}).ToList();

            return currlist.OrderBy(p => p.lCurrency).ToList();;
        }

Open in new window

try this

(delete 1 of the return statments depending on which you want to use)

string[] text;

            if (!File.Exists(currURI)) return new List<Currency>();

            using (var sr = new StreamReader(currURI))
            {
                text = sr.ReadToEnd().Replace(Environment.NewLine, string.Empty).Split(';');
            }


            var currlist = new List<Currency>();

            foreach (var part in text)
            {
                if (part != "")
                {
                    var parts = part.Split(',');
                    currlist.Add(new Currency { lCountry = parts[0].Trim(), lCurrency = parts[1].Trim() });
                }
            }

            // sort by lCountry
            return new List<Currency>(currlist.OrderBy(c => c.lCountry));

            //sort by lCurrency
            return new List<Currency>(currlist.OrderBy(c => c.lCurrency));

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RTSol

ASKER

Thanks guys - I finally got it going. I really learned a lot from thos - thanks again

-RTSol
Avatar of RTSol

ASKER

Hi again,

I just want to clarify a bit.

I just the line:
return new List<Currency>(currlist.OrderBy(c => c.lCurrency));
in my method. However, I also forgot to instanciate the list before the call - now it is:
        private List<CurrencyList> clist = new List<CurrencyList>();
        clist = currenList();

This was also something I didn't really had the hang of:

You have to define private classes in the right place inside the braces:

namespace TestApplication
{

      public class TestClass
      {
           
             private class MyClass
             {
             }

      }
}

Thanks again guys!
RTSol