0

I have a list of parameters:

List<double> parameters = new List<double>() { 3.56, 8.9, 1.7, 0.5, 4.69 };

And a list of generic objects:

List<object> objects= new List<object>() { objA, objB, objC, objD, objE };

How can I sort "objects" list accordingly with the sorting of "parameters" list?

5
  • Why not make a class? How'd we know which object goes with double and vice versa? Currently it doesn't make sense, if objA should go with 8.9 per say, then why isn't this relationship established in a way we could sort the list? Currently this question is lacking details for us to help you, please update. Commented Nov 21, 2020 at 19:43
  • @Codexer Both lists have same length. First is paired with first, second with second, etc... obviously (?) . Commented Nov 21, 2020 at 19:47
  • You said, How can I sort "objects" accordingly with the values of "parameters" ?, what are we sorting? If we sort objects, based on what exactly, then how do we know what index they go on in parameters? parameters object would need changed to match the objects list wouldn't it? If that's the case, how do we do that? Commented Nov 21, 2020 at 19:47
  • To be honest, you've not answered my questions (I'm asking for further details to help you), not updated your post to include what code you've tried and what isn't working. As already mentioned, please update your post to get help. Commented Nov 21, 2020 at 19:53
  • @Codexer I'm trying to keep my post as simple as possible. I can't possibly think why is not extremely obvious what I am asking for. Commented Nov 21, 2020 at 19:54

2 Answers 2

3

Although you accepted an answer, it's still fun to show a not a well-known method, but faster than any and a one-liner. But you have to start with arrays:

var parameters = new [] { 3.56, 8.9, 1.7, 0.5, 4.69 };
var objects = new [] { "objA", "objB", "objC", "objD", "objE" };

Array.Sort(parameters, objects);

Now objects is persistently sorted according to the sort order of parameters:

objD
objC
objA
objE
objB
Sign up to request clarification or add additional context in comments.

6 Comments

This is great too! I was desperate (some hour of search) so I promptly accepted the first working solution... Thanks for your alternative! Maybe I can change my code to use arrays...
Nice, I didn't know you could do that!
Does Array.Sort sort only "objects" array or also "parameters" array?
parameters too.
That's a problem. Ways to avoid that? Making a backup copy of original "parameters" array?
|
1

You can use the Linq function OrderBy to sort the objects using their respective index in the parameters list as a key.

List<double> parameters = new List<double>() { 3.56, 8.9, 1.7, 0.5, 4.69 };
List<object> objects = new List<object>() { objA, objB, objC, objD, objE };

objects = objects.OrderBy(x => parameters[objects.IndexOf(x)]).ToList();

5 Comments

I can't understand that! It is working fine... but.. can you explain how/why it works? ... your c#-fu si strong!
@maje90 Sorry, I should've made it more clear. I used the function OrderBy which takes in a lambda function. The variable 'x' on the left side of the function represents each item in the list, and it finds the index of that item in the 'objects' list. The item at that index in the 'parameters' list is used as the value when it's sorting the list. I'm sorry if I'm not making much sense - lambda and Linq is a bit confusing but when you get the hang of it it is incredibly useful.
I really need to make Linq mine. I'll preciously study you last comment. XD ... Thanks again!!
@Cosmoleon new List<object() isn't valid, maybe a type-o?
@Codexer Oops, that was a mistake

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.