Question Order By List Duplicates Keys

Celso Miranda

New member
Joined
Aug 19, 2021
Messages
3
Programming Experience
1-3
I have the following list
C#:
    IList studentTeste = new List() {
     new Student() { Id = 1, StudentId = 1, StudentName = "John"} ,
     new Student() { Id = 2, StudentId = 1, StudentName = "John"} ,
     new Student() { Id = 3, StudentId = 2, StudentName = "Ram"} ,
     new Student() { Id = 4, StudentId = 2, StudentName = "Ram"} ,
     new Student() { Id = 5, StudentId = 3, StudentName = "Bill"} ,
     new Student() { Id = 6, StudentId = 3, StudentName = "Bill"}
    };
I need to sort this list by prioritizing the StudentId that has duplicates in the list, as follows:

Result:

{ StudentId:1, StudentId:2, StudentId:3, StudentId:1, StudentId:2, StudentId:3 }

Help!
 
Last edited by a moderator:
This question is a fine example of why beginner programmers have trouble writing code. They try to go from an idea to code in one hop, without first gaining an understanding of what the code actually has to do, i.e. the steps to get to a result rather than just the result itself. You've provided a single example for a single specific data set. Code doesn't implement one example. It implements the logic for a set of rules. What are the rules here? Do you know? You certainly haven't told us if you do. If I had to guess - I do - then I'd say that you the list ordered by the lowest Id value for each distinct StudentId value, then by the next lowest Id value for the remaining StudentId values and so on. Is that correct?
 
Perhaps you mean GroupBy() rather than Sort() by.

On the other hand, your expected result doesn't really make sense. As @jmcilhinney mentions above, it's unclear exactly what your ordering rules are.
 
This question is a fine example of why beginner programmers have trouble writing code. They try to go from an idea to code in one hop, without first gaining an understanding of what the code actually has to do, i.e. the steps to get to a result rather than just the result itself. You've provided a single example for a single specific data set. Code doesn't implement one example. It implements the logic for a set of rules. What are the rules here? Do you know? You certainly haven't told us if you do. If I had to guess - I do - then I'd say that you the list ordered by the lowest Id value for each distinct StudentId value, then by the next lowest Id value for the remaining StudentId values and so on. Is that correct?

Hi, sorry for the simple explanation, I'll try to be more explanatory:

I have a list that has the StudentId that can repeat N times, what I need to do first is identify which are the distinct StudentIds in the list, so far so good. Now I need to sort this list in such a way that the StudentsIds don't remain sequentially like this: {1,1,2,2,3,3) I need them to be in a repeating way, like this: {1,2,3 ,1,2,3}

sorry for my english too, i'm not fluent.

Thank you so much for the help!
 
You're still just providing a single example of an input and the corresponding output, not an explanation of the steps required to get from any input to the appropriate output. Again, this is why you can't write code to do this: because you don't what it is that you have to do. Put down your keyboard and pick up a pen and paper and do it manually, for multiple inputs. Work out what the steps are that you need to perform and formalise that logic into an algorithm. Test that algorithm manually with different inputs. Once you have a working algorithm, then you can think about writing code. You don't even need to know what the original problem was to write the code. You only need to know the algorithm because the code implements that directly. Until you have that algorithm, you haven't really tried to solve the problem. You don't need any programming experience to write an algorithm. You just need to think logically. Programming experience can help you think logically, but you can at least try. Try to come up with an algorithm and get back to us when you have something or have a specific issue with a specific step.
 
You're still just providing a single example of an input and the corresponding output, not an explanation of the steps required to get from any input to the appropriate output. Again, this is why you can't write code to do this: because you don't what it is that you have to do. Put down your keyboard and pick up a pen and paper and do it manually, for multiple inputs. Work out what the steps are that you need to perform and formalise that logic into an algorithm. Test that algorithm manually with different inputs. Once you have a working algorithm, then you can think about writing code. You don't even need to know what the original problem was to write the code. You only need to know the algorithm because the code implements that directly. Until you have that algorithm, you haven't really tried to solve the problem. You don't need any programming experience to write an algorithm. You just need to think logically. Programming experience can help you think logically, but you can at least try. Try to come up with an algorithm and get back to us when you have something or have a specific issue with a specific step.
ok, i'll do as you said, thank you for your attention. Have a great day!
 
We can certainly help further if you get stuck but we just like to see you do what you can for yourself first. It's a form of tough love because learning to formulate an algorithm before writing code will help you a lot in the future.
 
Back
Top Bottom