KKK
KKK

Reputation: 11

sorting elements of a list

how can i 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");

Upvotes: 1

Views: 136

Answers (4)

Jaimal Chohan
Jaimal Chohan

Reputation: 8645

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

var orderdList = lstStr.OrderByDescsending();

Upvotes: 3

Andrew Orsich
Andrew Orsich

Reputation: 53705

You can easy sort by descending using linq:

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

Upvotes: 1

zerkms
zerkms

Reputation: 255155

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

Upvotes: 4

scatman
scatman

Reputation: 14565

you can sort then reverse:

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

Upvotes: 4

Related Questions