Prod.: Engine, ver.: 6018, ID: 60001245, Wish : Method to sort items of a collection

Wish : Method to sort items of a collection

Article60001245
TypeWish
ProductEngine
Version6018
Date Added10/13/2010
FixedYes [10/13/2010]
Submitted bystavros meimaroglou
Keywords

Subject

Method to sort items of a collection

Summary

Method to sort items of a collection

Solution

In version 6019 new sort methods was added in vdArray object and vdEntities collection

 

VectorDraw.Generics.vdArray<T> sort methods
        /// <summary>
        /// Sorts the elements in an entire collection using the <see cref="System.IComparable<T>"/>
        ///     generic interface implementation of each element of the System.Array.
        /// </summary>
        /// <exception cref="System.InvalidOperationException">One or more elements in array do not implement the <see cref="System.IComparable<T>"/>  generic interface.</exception>
        public void Sort()

        example:
        sort items in a collection of doubles
                vdArray<double> collectionOfDoubles = new vdArray<double>();
                collectionOfDoubles.AddItem(5);
                collectionOfDoubles.AddItem(15);
                collectionOfDoubles.AddItem(2);
                collectionOfDoubles.AddItem(8);
                collectionOfDoubles.AddItem(3);

                //sort the items from smaller value to bigger
                collectionOfDoubles.Sort();
                //the values will be take the following order
                //2 ,3, 5, 8, 15

 


        /// <summary>
        ///  Sorts the elements in a range of elements in entire collection using the specified
        ///     <see cref="System.Collections.Generic.IComparer<T>"/> generic interface.
        /// </summary>
        /// <param name="comparer">
        /// The <see cref="System.Collections.Generic.IComparer<T>"/> generic interface implementation
        ///     to use when comparing elements, or null to use the <see cref="System.IComparable<T>"/>
        ///     generic interface implementation of each element.
        /// </param>
        /// <exception cref="System.ArgumentException">
        /// The implementation of comparer caused an error during the sort.
        /// For example, comparer might not return 0 when comparing an item with itself.
        /// </exception>
        /// <exception cref="System.InvalidOperationException">
        /// comparer is null, and one or more elements in array do not implement the <see cref="System.IComparable<T>"/>  generic interface
        /// </exception>
        public void Sort(IComparer<T> comparer)
        example:
        sort items in a collection of gPoints depend of their distance from an origin point.


                 private class OriginComparer : System.Collections.Generic.IComparer<gPoint>
                 {
                    gPoint mOrigin;
                    public OriginComparer(gPoint Origin)
                    {
                        mOrigin = Origin;
                    }
                    public int Compare(gPoint x, gPoint y)
                    {
                        double dif = x.Distance3D(mOrigin) - y.Distance3D(mOrigin);
                        if (dif == 0.0d) return 0;
                        if (dif > 0.0d) return 1;
                        else return -1;
                    }
                 }
        
                vdArray<gPoint> collectionOfPoints = new vdArray<gPoint>();
                collectionOfPoints.AddItem(new gPoint(5,0,0));
                collectionOfPoints.AddItem(new gPoint(15, 0, 0));
                collectionOfPoints.AddItem(new gPoint(2, 0, 0));
                collectionOfPoints.AddItem(new gPoint(8, 0, 0));
                collectionOfPoints.AddItem(new gPoint(3, 0, 0));

                //sort the items from smaller to bigger depend of their distance from point(0,0,0)
                collectionOfPoints.Sort(new OriginComparer(new gPoint(0, 0, 0)));
                //the values will be take the following order
                //(2,0,0) ,(3,0,0), (5,0,0), (8,0,0), (15,0,0)


VectorDraw.Professional.vdCollections.vdEntities sort methods
        /// <summary>
        ///  Sorts the elements in a range of elements in entire collection using the specified
        ///     <see cref="System.Collections.Generic.IComparer<vdFigure>"/> generic interface.
        /// </summary>
        /// <param name="comparer">
        /// The <see cref="System.Collections.Generic.IComparer<vdFigure>"/> generic interface implementation
        ///     to use when comparing elements, or null to use the <see cref="System.IComparable<vdFigure>"/>
        ///     generic interface implementation of each element.
        /// </param>
        /// <exception cref="System.ArgumentException">
        /// The implementation of comparer caused an error during the sort.
        /// For example, comparer might not return 0 when comparing an item with itself.
        /// </exception>
        /// <exception cref="System.InvalidOperationException">
        /// comparer is null, and one or more elements in array do not implement the <see cref="System.IComparable<vdFigure>"/>  generic interface
        /// </exception>
        public void Sort(IComparer<vdFigure> comparer)
        example:
        sort items in vdEntities collection depend of their Layer name.
                  //a class object that used to compare to vdFigure depend of the Layer Name
                  private class FigureLayerComparer : System.Collections.Generic.IComparer<vdFigure>
                  {
                    public FigureLayerComparer()
                    {
                    }
                    public int Compare(vdFigure fig1, vdFigure fig2)
                    {
                        return System.StringComparer.InvariantCultureIgnoreCase.Compare(fig1.Layer.ToString(), fig2.Layer.ToString());
                    }
                  }
                //Create 4 layers and add them into the Layers collection of a vdDocument object
                vdLayer lay1 = doc.Layers.Add("1");
                vdLayer lay2 = doc.Layers.Add("2");
                vdLayer lay3 = doc.Layers.Add("3");
                vdLayer lay4 = doc.Layers.Add("4");

                //create a new vdLine object that belong to Layer with name "3"
                vdLine l1 = new vdLine();
                l1.SetUnRegisterDocument(doc);
                l1.setDocumentDefaults();
                l1.Layer = lay3;

               //create a new vdLine object that belong to Layer with name "1"
                vdLine l2 = new vdLine();
                l2.SetUnRegisterDocument(doc);
                l2.setDocumentDefaults();
                l2.Layer = lay1;

                //create a new vdLine object that belong to Layer with name "4"
                vdLine l3 = new vdLine();
                l3.SetUnRegisterDocument(doc);
                l3.setDocumentDefaults();
                l3.Layer = lay4;

                //create a new vdLine object that belong to Layer with name "2"
                vdLine l4 = new vdLine();
                l4.SetUnRegisterDocument(doc);
                l4.setDocumentDefaults();
                l4.Layer = lay2;

                //add the new vdlines to vdDocument Model Entities collection
                doc.Model.Entities.AddItem(l1);
                doc.Model.Entities.AddItem(l2);
                doc.Model.Entities.AddItem(l3);
                doc.Model.Entities.AddItem(l4);

                //change the order of items in the collection so the items of layer "1" to be first in the collection folow by items in layer "2" , "3" and "4" at the end of the collection
                doc.Model.Entities.Sort(new FigureLayerComparer());