1

I need to sort one list (string) according to another list values (int) from the smallest to the highest int. Possibility to have several times the same string.

For example:

List<string> list1= new List<string>() { "abc", "xyzz", "abc", "fgh", "abc" };

List<int> list2 = new List<int>() { 8, 15, 4, 2, 22 };

output:

list1 {"fgh","abc", "abc", "xyzz", "abc"}

This line should probably work but I still miss something:

list1 = list1.OrderBy(d => list2."i miss something here".ToList());
8
  • It's not clear what you are trying to do. How would you like your list2 to affect the ordering of list1? Commented May 15, 2021 at 7:05
  • from the smallest int to the highest int , put in order Commented May 15, 2021 at 7:06
  • the first int determines the first string, second int second string and so on. Commented May 15, 2021 at 7:07
  • 1
    Something like list1.Select((x, i) => (Item: x, Index: i)).OrderBy(t => list2[t.Index]).Select(t => t.Item).ToList() ought to work. In any case, see duplicates...there are lots of examples on the site already you can draw from. Commented May 15, 2021 at 7:21
  • 1
    It might worked, but is not the best. Use this answer stackoverflow.com/a/13144084/5202563 by Servy based on Zip Commented May 15, 2021 at 7:37

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.