multiple sort criterias for any class; arraylist performance

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • herbert

    multiple sort criterias for any class; arraylist performance

    1) I created a class Person whose objects are stored in collections.
    Using IComparable allows to define one sort criteria like name.
    Best practice: How can I add more sort criterias like age, zip, ...?
    I don't care whether the original collection is sorted or I get a copied
    collection sorted.

    2) VB.NET: When is an ArrayList faster than static arrays which grow using
    Redim now and then?

    thanks herbert
  • Jorge L Matos [MCSD.NET]

    #2
    RE: multiple sort criterias for any class; arraylist performance

    1) You might create a class that generically sorts by using reflection.

    class GenericSorter : IComparer
    {
    string mFieldName;
    public GenericSorter(s tring fieldName)
    {
    mFieldName = fieldName;
    }

    public int Compare(object x, object y)
    {
    //Homework assignment
    //Use reflection to compare object x to object y using the mFieldName
    field
    }
    }

    usage:

    GenericSorter sorter = new GenericSorter(" Zip");


    2) You'll have to use some test code to find out for sure but I think the
    difference is neglible.

    "herbert" wrote:
    [color=blue]
    > 1) I created a class Person whose objects are stored in collections.
    > Using IComparable allows to define one sort criteria like name.
    > Best practice: How can I add more sort criterias like age, zip, ...?
    > I don't care whether the original collection is sorted or I get a copied
    > collection sorted.
    >
    > 2) VB.NET: When is an ArrayList faster than static arrays which grow using
    > Redim now and then?
    >
    > thanks herbert[/color]

    Comment

    • Jorge L Matos [MCSD.NET]

      #3
      RE: multiple sort criterias for any class; arraylist performance

      1) You might create a class that generically sorts by using reflection.

      class GenericSorter : IComparer
      {
      string mFieldName;
      public GenericSorter(s tring fieldName)
      {
      mFieldName = fieldName;
      }

      public int Compare(object x, object y)
      {
      //Homework assignment
      //Use reflection to compare object x to object y using the mFieldName
      field
      }
      }

      usage:

      GenericSorter sorter = new GenericSorter(" Zip");


      2) You'll have to use some test code to find out for sure but I think the
      difference is neglible.

      "herbert" wrote:
      [color=blue]
      > 1) I created a class Person whose objects are stored in collections.
      > Using IComparable allows to define one sort criteria like name.
      > Best practice: How can I add more sort criterias like age, zip, ...?
      > I don't care whether the original collection is sorted or I get a copied
      > collection sorted.
      >
      > 2) VB.NET: When is an ArrayList faster than static arrays which grow using
      > Redim now and then?
      >
      > thanks herbert[/color]

      Comment

      • herbert

        #4
        RE: multiple sort criterias for any class; arraylist performance

        Sorry Jorge, I don't understand you. For me reflection is to read type info
        from assemblies.

        I write in VB.NET. My class looks like this:
        Class Person
        Dim strName as String
        Dim intAge As Integer
        End Class

        I create objects of this class and add them to an ArrayList. Then I want to
        sort the ArrayList by Name and/or Age.
        I included a CompareTo() Method implementing IComparable for default sort -
        it works.

        However I do not understand the online help which might tell me I should
        implement IComparer too. However the IComparer Interface is implemented
        outside the Person class which confuses me...

        Can somebody pls give me the complete code to sort Persons by different
        criteria?

        thanks herbert

        Comment

        • herbert

          #5
          RE: multiple sort criterias for any class; arraylist performance

          Sorry Jorge, I don't understand you. For me reflection is to read type info
          from assemblies.

          I write in VB.NET. My class looks like this:
          Class Person
          Dim strName as String
          Dim intAge As Integer
          End Class

          I create objects of this class and add them to an ArrayList. Then I want to
          sort the ArrayList by Name and/or Age.
          I included a CompareTo() Method implementing IComparable for default sort -
          it works.

          However I do not understand the online help which might tell me I should
          implement IComparer too. However the IComparer Interface is implemented
          outside the Person class which confuses me...

          Can somebody pls give me the complete code to sort Persons by different
          criteria?

          thanks herbert

          Comment

          • Helge Jensen

            #6
            Re: multiple sort criterias for any class; arraylist performance

            herbert wrote:[color=blue]
            > 1) I created a class Person whose objects are stored in collections.
            > Using IComparable allows to define one sort criteria like name.
            > Best practice: How can I add more sort criterias like age, zip, ...?
            > I don't care whether the original collection is sorted or I get a copied
            > collection sorted.[/color]

            Write external IComparer implementations , and use Array.Sort to sort:

            public class AgeComparer: IComparer {
            public static AgeComparer Global = new AgeComparer;
            public int Compare(object x, object y) {
            return ((Person)x).Age - ((Person)y).Age ;
            }
            }
            Person[] persons = ...;
            System.Array.So rt(persons, AgeComparer.Glo bal);

            or make a dictionary using sorted-list:

            class AgeSorted: SortedList {
            public AgeSorted(int count): base(AgeCompare r.Global, count) {}
            }
            IDictionary ages = new AgeSorted();
            ...
            foreach ( Person p in ages.Keys ) /* in age order */
            f(p);

            Note that SortedList seems to be pretty slow when items are inserted
            out-of-order, possibly because it's based on an Array. If that's a
            problem for you there is really no remedy except implementing you own
            sorted data-structure using a tree.
            [color=blue]
            > 2) VB.NET: When is an ArrayList faster than static arrays which grow using
            > Redim now and then?[/color]

            ArrayList would probably be faster all the time... try making a test :)

            --
            Helge Jensen
            mailto:helge.je nsen@slog.dk
            sip:helge.jense n@slog.dk
            -=> Sebastian cover-music: http://ungdomshus.nu <=-

            Comment

            • Helge Jensen

              #7
              Re: multiple sort criterias for any class; arraylist performance

              herbert wrote:[color=blue]
              > 1) I created a class Person whose objects are stored in collections.
              > Using IComparable allows to define one sort criteria like name.
              > Best practice: How can I add more sort criterias like age, zip, ...?
              > I don't care whether the original collection is sorted or I get a copied
              > collection sorted.[/color]

              Write external IComparer implementations , and use Array.Sort to sort:

              public class AgeComparer: IComparer {
              public static AgeComparer Global = new AgeComparer;
              public int Compare(object x, object y) {
              return ((Person)x).Age - ((Person)y).Age ;
              }
              }
              Person[] persons = ...;
              System.Array.So rt(persons, AgeComparer.Glo bal);

              or make a dictionary using sorted-list:

              class AgeSorted: SortedList {
              public AgeSorted(int count): base(AgeCompare r.Global, count) {}
              }
              IDictionary ages = new AgeSorted();
              ...
              foreach ( Person p in ages.Keys ) /* in age order */
              f(p);

              Note that SortedList seems to be pretty slow when items are inserted
              out-of-order, possibly because it's based on an Array. If that's a
              problem for you there is really no remedy except implementing you own
              sorted data-structure using a tree.
              [color=blue]
              > 2) VB.NET: When is an ArrayList faster than static arrays which grow using
              > Redim now and then?[/color]

              ArrayList would probably be faster all the time... try making a test :)

              --
              Helge Jensen
              mailto:helge.je nsen@slog.dk
              sip:helge.jense n@slog.dk
              -=> Sebastian cover-music: http://ungdomshus.nu <=-

              Comment

              • herbert

                #8
                Re: multiple sort criterias for any class; arraylist performance

                Thanks Helge,
                the code works and in VB.NET it's as short as this:

                Public Class AgeComparer
                Implements IComparer
                Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer
                _ Implements System.Collecti ons.IComparer.C ompare
                Return CType(x, ClsPerson).Age - CType(y, ClsPerson).Age
                End Function

                Usage:
                'sort by age
                Dim myComparer As New AgeComparer
                myAL.Sort(myCom parer)

                thanks!

                Comment

                • herbert

                  #9
                  Re: multiple sort criterias for any class; arraylist performance

                  Thanks Helge,
                  the code works and in VB.NET it's as short as this:

                  Public Class AgeComparer
                  Implements IComparer
                  Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer
                  _ Implements System.Collecti ons.IComparer.C ompare
                  Return CType(x, ClsPerson).Age - CType(y, ClsPerson).Age
                  End Function

                  Usage:
                  'sort by age
                  Dim myComparer As New AgeComparer
                  myAL.Sort(myCom parer)

                  thanks!

                  Comment

                  Working...