2

I have a list1 like this :

{A,B,C,D,E,F} 

I have another list2 that list2 count is equal with list1 count (6=6) :

{50,100,14,57,48,94} 

I want sort list1 according to list2, that list2 to be sorted ascending.

{14,48,50,57,94,100} 

as a result :

{C,E,A,D,F,B} 

I used the following code. But the result is not sorted

list1= list1.OrderBy(d => list2.IndexOf(d)).ToList(); 
4
  • 2
    I read this twice and I still have no idea what you are asking. Maybe I have missed something... Commented Jun 5, 2018 at 14:41
  • 2
    How do the elements in your two lists rely on each other? Commented Jun 5, 2018 at 14:42
  • @HimBromBeereIn another function, they are assigned and rely on each other Commented Jun 5, 2018 at 14:44
  • Read this stackoverflow.com/questions/9280054/… Commented Jun 5, 2018 at 14:45

5 Answers 5

5

Why not use a sorted dictionary?

var list1 = new List<int> { 50, 100, 14, 57, 48, 94 } var list2 = new List<string> { "A", "B", "C", "D", "E", "F" }; var dict = new SortedDictionary<int, string>(); for (int i = 0; i < list1.Count; i++) { dict.Add(list1[i], list2[i]); } 

You'll now be able to access the values in the correct order.

Sign up to request clarification or add additional context in comments.

2 Comments

Comparable to my solution, but in a single step. Definitly better
@mm8 The OP isn't simply sorting a list, they're sorting it based on other criteria.Plus, this allows them to access the data based on an index later.
4

You can do this directly if you use an array instead of a list:

string[] list1 = { "A", "B", "C", "D", "E", "F" }; int[] list2 = { 50, 100, 14, 57, 48, 94 }; Array.Sort(list2, list1); Console.WriteLine(string.Join(", ", list1)); // C, E, A, D, F, B 

The Array.Sort(Array keys, Array items) method is provided for this exact purpose.

Sorts a pair of one-dimensional Array objects (one contains the keys and the other contains the corresponding items) based on the keys in the first Array using the IComparable implementation of each key.

Sadly, there is no equivalent for List<T>.

Comments

4

You could use Enumerable.Zip to combine them, then OrderBy to order

Applies a specified function to the corresponding elements of two sequences, producing a sequence of the results.

var first = new List<char>() { 'A', 'B', 'C', 'D', 'E', 'F' }; var second = new List<double>() { 50, 100, 14, 57, 48, 94 }; var results = first.Zip(second, (f, s) => (first: f, second: s)) .OrderBy(x => x.second) .Select(x => x.first); Console.WriteLine(string.Join(", ", results )); 

Output

C, E, A, D, F, B 

1 Comment

It worth saying that for arrays, there is a built-in method to solve this specific problem: learn.microsoft.com/fr-fr/dotnet/api/…
0

Try this:

int index = 0; list1 = list1.OrderBy(d => list2[index++]).ToList(); 

It should produce the expected result given the following two lists:

List<string> list1 = new List<string> { "A", "B", "C", "D", "E", "F" }; List<int> list2 = new List<int> { 50, 100, 14, 57, 48, 94 }; 

Comments

0

Try this:

docs = docs.OrderBy(d => docsIds.IndexOf(d.Id)).ToList(); 

should give the expected result.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.