/// <summary>
        /// <para>Sorts the IAtomContainers in the given IAtomContainerSet by the following
        /// criteria with decreasing priority:</para>
        /// <list type="bullet">
        ///   <item>Compare atom count</item>
        ///   <item>Compare molecular weight (heavy atoms only)</item>
        ///   <item>Compare bond count</item>
        ///   <item>Compare sum of bond orders (heavy atoms only)</item>
        /// </list>
        /// <para>If no difference can be found with the above criteria, the IAtomContainers are
        /// considered equal.</para>
        /// </summary>
        /// <param name="atomContainerSet">The collection of IAtomContainer objects</param>
        public static void Sort <T>(IChemObjectSet <T> atomContainerSet) where T : IAtomContainer
        {
            var atomContainerList = atomContainerSet.ToList();

            atomContainerList.Sort(new AtomContainerComparator <T>());
            atomContainerSet.Clear();
            foreach (var anAtomContainerList in atomContainerList)
            {
                atomContainerSet.Add(anAtomContainerList);
            }
        }
        internal IAtomContainer Generate2(IChemObjectSet <IAtomContainer> atomContainers)
        {
            int  iteration      = 0;
            bool structureFound = false;

            do
            {
                iteration++;
                bool bondFormed;
                do
                {
                    bondFormed = false;
                    var atomContainersArray = atomContainers.ToList();
                    for (var atomContainersArrayIndex = 0; atomContainersArrayIndex < atomContainersArray.Count; atomContainersArrayIndex++)
                    {
                        var ac = atomContainersArray[atomContainersArrayIndex];
                        if (ac == null)
                        {
                            continue;
                        }

                        var atoms = ac.Atoms.ToList(); // ToList is required because some atoms are added to ac.Atoms in the loop.
                        foreach (var atom in atoms)
                        {
                            if (!satCheck.IsSaturated(atom, ac))
                            {
                                var partner = GetAnotherUnsaturatedNode(atom, ac, atomContainers);
                                if (partner != null)
                                {
                                    var toadd = AtomContainerSetManipulator.GetRelevantAtomContainer(atomContainers, partner);
                                    var cmax1 = satCheck.GetCurrentMaxBondOrder(atom, ac);
                                    var cmax2 = satCheck.GetCurrentMaxBondOrder(partner, toadd);
                                    var max   = Math.Min(cmax1, cmax2);
                                    var order = Math.Min(Math.Max(1, max), 3); //(double)Math.Round(Math.Random() * max)
                                    Debug.WriteLine($"cmax1, cmax2, max, order: {cmax1}, {cmax2}, {max}, {order}");
                                    if (toadd != ac)
                                    {
                                        var indexToRemove = atomContainersArray.IndexOf(toadd);
                                        if (indexToRemove != -1)
                                        {
                                            atomContainersArray[indexToRemove] = null;
                                        }
                                        atomContainers.Remove(toadd);
                                        ac.Add(toadd);
                                    }
                                    ac.Bonds.Add(ac.Builder.NewBond(atom, partner, BondManipulator.CreateBondOrder(order)));
                                    bondFormed = true;
                                }
                            }
                        }
                    }
                } while (bondFormed);
                if (atomContainers.Count == 1 &&
                    satCheck.IsSaturated(atomContainers[0]))
                {
                    structureFound = true;
                }
            } while (!structureFound && iteration < 5);
            if (atomContainers.Count == 1 && satCheck.IsSaturated(atomContainers[0]))
            {
                structureFound = true;
            }
            if (!structureFound)
            {
                return(null);
            }
            return(atomContainers[0]);
        }