开发者

sorting elements of a list

开发者 https://www.devze.com 2023-02-17 04:03 出处:网络
how can i开发者_StackOverflow社区 use .Sort() to sort a list in reverse order: List<string> lstStr = new List<string>() { \"bac\", \"abc\", \"cba\" };

how can i开发者_StackOverflow社区 use .Sort() to sort a list in reverse order:

List<string> lstStr = new List<string>() { "bac", "abc", "cba" };
lstStr.Sort(); //i want something like Sort("desc");


you can sort then reverse:

List<string> lstStr = new List<string>() { "bac", "abc", "cba" };
lstStr.Sort();
lstStr.Reverse();


lstStr.Sort((x, y) => string.Compare(y, x));


in .Net 3.5, using Linq, you are able to write

var orderdList = lstStr.OrderByDescsending();


You can easy sort by descending using linq:

var lstStr = new List<string>() { "bac", "abc", "cba" };
lstStr = lstStr.OrderByDescending(x => x).ToList();
0

精彩评论

暂无评论...
验证码 换一张
取 消