RTSol
asked on
Sorting a List<Currency>
Hi,
I have a list of currencies I need to sort. How can that be done?
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;
}
Whatever calls this method should then be able to invoke the Sort() method inherent to List<>
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.
currlist.Sort() doesn'r work.
All you need to do is call the List<T>.Sort() function:
See code
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;
}
Sorry, Got your post after I had posted.
If .Sort() does not work then use:
If .Sort() does not work then use:
currlist.Sort(new Comparison<Currency>(Currency.ToDecimal());
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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<Curr encyList> so if I use this I need to convert Sorted to System.Collections.Generic .List<Curr encyList>.
Any ideas?
The following line produces a sorted list
var Sorted = currlist.OrderBy(p => p.lCurrency);
but this is not of the type System.Collections.Generic
Any ideas?
Ooh, a lambda expression would mean that you can sort with LINQ...
var Sorted = currlist.OrderBy(p => p.lCurrency).ToList();
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*.
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
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
}
}
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;}
}
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() });
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();;
}
try this
(delete 1 of the return statments depending on which you want to use)
(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));
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks guys - I finally got it going. I really learned a lot from thos - thanks again
-RTSol
-RTSol
ASKER
Hi again,
I just want to clarify a bit.
I just the line:
return new List<Currency>(currlist.Or derBy(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
I just want to clarify a bit.
I just the line:
return new List<Currency>(currlist.Or
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