sort lists

-Nono-

New member
Joined
Mar 28, 2023
Messages
3
Programming Experience
Beginner
Hello again, it's me again, I have another problem,
see you I have two lists which happen to be in the same list, and infact the thing is that I would like to sort one of the two lists (sublist in quotes) but here I do not know how to do it,
the code looks like this

C#:
private static List<Bidule> BubbleSortDiagnostics(Bidule[] element) // Bidule[] element
        {
            int cnt = element.Length;
            bool sorted = false;
            while (!sorted)
            {
                sorted = true;
                for (int i = 0; i < cnt - 1; i++)
                {
                    if (string.Compare(element[i].DisplayName.ToString(), element[i + 1].DisplayName.ToString()) > 0) // Si l'élément [i] est plus petit que l'élément[i+1] et supérieur à 0 effectuer :
                    {
                        Bidule t = element[i];
                        element[i] = element[i + 1];
                        element[i + 1] = t;
                        sorted = false;
                    }
                }

                cnt -= 1;
            }
            return element.ToList();

public List<Bidule> InstrumentElements
        {

            get
            {
                if (_instrumentElements == null)
                    if (FrameworkController.Instance.Instrument.Configuration != null)
                    {

                        _instrumentElements = FrameworkController.Instance.Instrument.Configuration.AllNestedItems.OfType<Bidule>().ToList();

                        _instrumentElements.ForEach(element => Console.WriteLine(InstrumentElements));

                        var toto = from element in _instrumentElements
                                   orderby element ascending
                                   select _instrumentElements;


                        List<Bidule> Trcg3list = new List<Bidule>();
                        List<Bidule> Restlist = new List<Bidule>();

                        foreach (Bidule element in _instrumentElements)
                        {
                            if (element.DisplayName.ToString().Contains("MF Trcg3"))
                            {


                                Trcg3list.Add(element);
                            }
                            else
                            {
                                Restlist.Add(element);
                            }
                            
                        }

                        List<Bidule> listOfLists = new List<Bidule>();

                        _instrumentElements.Clear();

                        _instrumentElements.AddRange(Restlist);
                        _instrumentElements.AddRange(Trcg3list);

                        //BubbleSort(_instrumentElements.ToArray());
                        Trcg3list.Sort(); // Trie la liste Trcg3
                        Restlist.Sort(); // Trie la liste du reste

                        Restlist = BubbleSortDiagnostics(Trcg3list.ToArray());
                        Trcg3list = BubbleSortDiagnostics(Trcg3list.ToArray());
                        //_instrumentElements = BubbleSortDiagnostics(_instrumentElements.ToArray());
                        //_instrumentElements = BubbleSortDiagnostics(Trcg3list.ToArray());
                        

                        DoPropertyChanged(SelectedItemPropertyName);
                        DoPropertyChanged(InstrumentElementsPropertyName);
                    }
                                return _instrumentElements;

            }
        }
 
I see that you're calling Sort. Presumably that is not meeting your needs for some reason. Would you care to explain the problem, rather than our having to work it out for ourselves?
 
*sigh* In your other thread, you said that you solved it and implied that you chose to make a list of lists. In your code above you have a variable called listOfLists on line 59, but it is actually not a list of lists. It's just a list of elements. A list of lists would look like List<List<Bidule>>, but you have a List<Bidule>.

Anyway, to me, the problem stems from that misunderstanding. Since you call AddRange() on lines 63-64, you are just copying references to Bidule from your two original lists RestList and TrgcList. After the element references have been copied, it doesn't matter how else you might rearrange the references in your RestList and TrgcList. _instrumentElements will still have the original order the references were in when you called AddRange().

Now, on the other hand, if you had sorted your list(s) before adding to _instrumentElements, then _instrumentElements woud pick up that sorted order.

As an aside, why roll your own sorting? If rolling your own sorting, why bubble sort of all the sorting algorithms to choose from?
 
Back
Top Bottom