2

I am implementing Data Mining Algorithm in .NET 4.0 (C#) and LINQ will work and I need some helps.

I have a List A and a Dictionary B. How to sort A by value B. For example A = {b, d, c} and B = {(b,2),(c,5),(d,1),(e,3)}. I need sort A -> A = {c, b, d}.

2 Answers 2

9

You are asking for OrderBy or OrderByDescending extensions:

List<string> A = ...
Dictionary<string, int> B = ...

A = A.OrderByDescending(a => B[a]).ToList()

Or using Sort method:

A.Sort((x, y) => B[y].CompareTo(B[x]));
Sign up to request clarification or add additional context in comments.

2 Comments

you mean OrderByDescending i think
Upvoted for the addition, the in-line Sort of List<>.
1

Try this

           List<string> A = new List<string>() {"b", "d", "c"};
            Dictionary<string,int> B = new Dictionary<string,int>() {{"b",2},{"c",5},{"d",1},{"e",3}};

            List<string> results = A.AsEnumerable().Select(x => new { A = x, i = B[x] }).OrderByDescending(y => y.i).Select(z => z.A).ToList();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.