Reputation: 694
I have a list of a class like this :
public class CityCodeInfo
{
public string CityName;
public string Province;
public string Code;
}
List<CityCodeInfo> lstCityt = new List<CityCodeInfo>();
How can i sort this list by any of its variables (cityname, province and code)
i've tried this code:
lstCityt.Sort((x, y) => string.Compare(x.CityName, y.CityName));
but it doesn't work...
Any Idea?!
Upvotes: 2
Views: 13725
Reputation: 1064104
What you already have is already complete and correct:
lstCityt.Sort((x, y) => string.Compare(x.CityName, y.CityName));
That indeed sorts a list of a class. It sounds like you are seeing some secondary issue to do with the repeater, but you have not provided context for that. The main thing I'd look at is timing, i.e. whether you binding before or after sorting the list.
Upvotes: 2
Reputation: 14302
You can use LINQ for it.
Ascending Order
var result = lstCityt.OrderBy(C=> C.CityName).ThenBy(C=> C.Province).ThenBy(C=> C.Code);
Descending Order
var result = lstCityt.OrderByDescending(C=> C.CityName).ThenByDescending(C=> C.Province).ThenByDescending(C=> C.Code);
Both
var result = lstCityt.OrderBy(C=> C.CityName).ThenByDescending(C=> C.Province).ThenByDescending(C=> C.Code);
Upvotes: 9