❔ LinkedList

Bbialasik__1/10/2023
            List<string> people = new()
            {
                "Xavier", "Alex",
                "James", "Laura",
                "Adil", "Cheryl",
                "Charlie"
            };
            LinkedList<string> classList = new();

            char[] alpha = "bcdefghijklmnopqrstuvwxy".ToCharArray();
            foreach(string person in people)
            {
                LinkedListNode<string> firstNode = classList.First;
                person.ToLower();
                if (person.StartsWith('a'))
                {
                    firstNode = classList.AddFirst(person);
                }
                else if (person.StartsWith('z'))
                {
                    classList.AddLast(person);
                }
                else
                {
                    for (int i = 0; i < alpha.Length; i++)
                    {
                        if (person.StartsWith(alpha[i]))
                        {
                            if (firstNode == null)
                            {
                                firstNode = classList.AddFirst(person);
                            }
                            classList.AddAfter(firstNode, person);
                        }
                    }
                }
            }

I then tried:
            foreach(string person in people)
            {
                LinkedListNode<string> firstNode = classList.First;
                person.ToLower();
                if (person.StartsWith('a'))
                {
                    classList.AddFirst(person);
                }
                else
                {
                    for (int i = 0; i < alpha.Length; i++)
                    {
                        if (person.StartsWith(alpha[i]))
                        {
                            classList.AddLast(person);
                        }
                    }
                }
            }
Bbialasik__1/10/2023
I need to get names from a list and add them to a LinkedList# without using Sort() or OrderBy()
DdaVinki1/10/2023
If you’re trying to get the sorted linked list, you could sort the list yourself first and then turn it into a linked list
CChucklesTheBeard1/10/2023
your teacher is probably aiming for an "insertion sort" - that search term should help
AAccord1/11/2023
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.