Sorting objects, that are in an ArrayList?

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

    Sorting objects, that are in an ArrayList?

    So I have an ArrayList that gets populated with objects like:

    myAL.Add(new CustomObject(pa rm1,parm2));

    I'm consuming this ArrayList from an ObjectDataSourc e and would like to have
    this support sorting (because it's ultimately being consumed in a GridView).
    I can't simply sort the ArrayList (because it only knows it's holding a
    bunch of objects). So I need a way to sort the ArrayList, based on the
    data - that is within the objects that it has loaded.

    It seems like there must be some relatively simple way that I can make that
    CustomObject() class sortable... any ideas??? TIA


  • Wiktor Zychla [C# MVP]

    #2
    Re: Sorting objects, that are in an ArrayList?

    > It seems like there must be some relatively simple way that I can make[color=blue]
    > that CustomObject() class sortable... any ideas??? TIA[/color]

    you just implement IComparable for the default sort order or create an
    external [or inner] class that implements IComparer for any other
    [non-default] sort order.

    Wiktor Zychla


    Comment

    • Roland

      #3
      Re: Sorting objects, that are in an ArrayList?

      Hi

      I hope I understud your question in a correct way.

      You need to use the IComparable Interface. -> YourClass : IComparable
      In the method 'CompareTo(obje ct x)' which you have to implement you can
      compare anything you want and by returning a number less than zero you
      indicate that 'This Instance' is smaller than x, by return 0 you
      indicate that 'This Instance' is equal to x and a number bigger than
      zero indicates that 'This Instance' is bigger than x.
      But you are (can) defining the criteria in which case you return -1, 0
      or 1

      Finally you can call myArrayList.Sor t(...);

      see:



      Hope this helps
      Roland

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Sorting objects, that are in an ArrayList?

        RCS <rseder@gmail.c om> wrote:[color=blue]
        > So I have an ArrayList that gets populated with objects like:
        >
        > myAL.Add(new CustomObject(pa rm1,parm2));
        >
        > I'm consuming this ArrayList from an ObjectDataSourc e and would like to have
        > this support sorting (because it's ultimately being consumed in a GridView).
        > I can't simply sort the ArrayList (because it only knows it's holding a
        > bunch of objects). So I need a way to sort the ArrayList, based on the
        > data - that is within the objects that it has loaded.
        >
        > It seems like there must be some relatively simple way that I can make that
        > CustomObject() class sortable... any ideas??? TIA[/color]

        You can sort the ArrayList either by implementing IComparable within
        the class itself, or by passing an IComparer which can compare two
        CustomObjects into the call to ArrayList.Sort.

        --
        Jon Skeet - <skeet@pobox.co m>
        http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
        If replying to the group, please do not mail me too

        Comment

        • RCS

          #5
          Re: Sorting objects, that are in an ArrayList?

          Thank you guys for the response - and I'm sort of with you guys on these
          ideas (this was in the ballpark of what I was thinking, but I don't know how
          to implement this). I don't understand *how* I would actually determine how
          to sort my objects. Let me give a more thorough example...

          I have a class called FileObject - which is a class that represents exactly
          one file or directory, it has public get properties for filename, size,
          extension, etc, etc.. Now, my other class FileSystemDataO bject, populates an
          arraylist, full of these FileObject objects (one, per file in a directory),
          as it loops through System.IO.Direc tory.GetFiles(@ "C:\") - for example.

          So FileSystemDataO bject has a populated ArrayList - that contains many
          objects of type FileObject.

          So, are you saying I should implement IComparable in the FileObject class?
          If so - how does that help the containing ArrayList in sorting these
          objects? I think I just need one more tiny push to grasp this.. thanks
          much..


          "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
          news:MPG.1e07db 49789cd23698cb1 c@msnews.micros oft.com...[color=blue]
          > RCS <rseder@gmail.c om> wrote:[color=green]
          >> So I have an ArrayList that gets populated with objects like:
          >>
          >> myAL.Add(new CustomObject(pa rm1,parm2));
          >>
          >> I'm consuming this ArrayList from an ObjectDataSourc e and would like to
          >> have
          >> this support sorting (because it's ultimately being consumed in a
          >> GridView).
          >> I can't simply sort the ArrayList (because it only knows it's holding a
          >> bunch of objects). So I need a way to sort the ArrayList, based on the
          >> data - that is within the objects that it has loaded.
          >>
          >> It seems like there must be some relatively simple way that I can make
          >> that
          >> CustomObject() class sortable... any ideas??? TIA[/color]
          >
          > You can sort the ArrayList either by implementing IComparable within
          > the class itself, or by passing an IComparer which can compare two
          > CustomObjects into the call to ArrayList.Sort.
          >
          > --
          > Jon Skeet - <skeet@pobox.co m>
          > http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
          > If replying to the group, please do not mail me too[/color]


          Comment

          • RCS

            #6
            Re: Sorting objects, that are in an ArrayList?

            Does the caller (which I assume would be myArrayList.Sor t) - does that pass
            in another reference of my comparable class? In other words, let's say I
            have a class called User that has FirstName and LastName properties.. and
            User now implements IComparable.. and I now have to create public int
            CompareTo(objec t obj)

            Inside that function, is that another User object being sent in? And should
            I do something like this?

            User objUserFromOuts ide = (obj as User);
            if ( this.FirstName > objUserFromOuts ide.FirstName)
            return 1;
            else
            {
            if ( this.FirstName == objUserFromOuts ide.FirstName)
            return 0;
            else
            return -1;
            }


            If so - then how would I handle a sort of different fields (like lastname,
            address, city, etc)???? Thanks again


            "Roland" <roland.weiersm ueller@gmx.net> wrote in message
            news:1134411509 .815431.61780@g 49g2000cwa.goog legroups.com...[color=blue]
            > Hi
            >
            > I hope I understud your question in a correct way.
            >
            > You need to use the IComparable Interface. -> YourClass : IComparable
            > In the method 'CompareTo(obje ct x)' which you have to implement you can
            > compare anything you want and by returning a number less than zero you
            > indicate that 'This Instance' is smaller than x, by return 0 you
            > indicate that 'This Instance' is equal to x and a number bigger than
            > zero indicates that 'This Instance' is bigger than x.
            > But you are (can) defining the criteria in which case you return -1, 0
            > or 1
            >
            > Finally you can call myArrayList.Sor t(...);
            >
            > see:
            > http://msdn.microsoft.com/library/de...classtopic.asp
            > http://msdn.microsoft.com/library/de...aretotopic.asp
            >
            > Hope this helps
            > Roland
            >[/color]


            Comment

            • Chris Dunaway

              #7
              Re: Sorting objects, that are in an ArrayList?

              RCS wrote:[color=blue]
              > I don't understand *how* I would actually determine how
              > to sort my objects. Let me give a more thorough example...[/color]

              You don't have to know how to sort your objects. All you need to do is
              know how to compare two of them and return which one is smaller or
              'less than' the other. The ArrayList will do the sorting and it will
              call your CompareTo method which should return 1 if the first object is
              greater than the second, 0 if they are equal, and -1 if the first is
              less than the second.

              Your CompareTo method can use any approach you want. The code you
              posted earlier looked correct to me.
              [color=blue]
              > So, are you saying I should implement IComparable in the FileObject class?
              > If so - how does that help the containing ArrayList in sorting these
              > objects? I think I just need one more tiny push to grasp this.. thanks
              > much..[/color]

              Because the ArrayList will take two of your objects and call the
              CompareTo of one and pass in the other. Your CompareTo needs simply to
              return 1, 0, or -1 which indicates which is greater.

              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: Sorting objects, that are in an ArrayList?

                RCS <rseder@gmail.c om> wrote:[color=blue]
                > Thank you guys for the response - and I'm sort of with you guys on these
                > ideas (this was in the ballpark of what I was thinking, but I don't know how
                > to implement this). I don't understand *how* I would actually determine how
                > to sort my objects. Let me give a more thorough example...
                >
                > I have a class called FileObject - which is a class that represents exactly
                > one file or directory, it has public get properties for filename, size,
                > extension, etc, etc.. Now, my other class FileSystemDataO bject, populates an
                > arraylist, full of these FileObject objects (one, per file in a directory),
                > as it loops through System.IO.Direc tory.GetFiles(@ "C:\") - for example.
                >
                > So FileSystemDataO bject has a populated ArrayList - that contains many
                > objects of type FileObject.
                >
                > So, are you saying I should implement IComparable in the FileObject class?
                > If so - how does that help the containing ArrayList in sorting these
                > objects? I think I just need one more tiny push to grasp this.. thanks
                > much..[/color]

                You could implement IComparable in the FileObject class, and then just
                call Sort() on the ArrayList. Alternatively, you could implement
                IComparer in another class to compare two FileObject instances, and
                then call ArrayList.Sort( IComparer) with an instance of the IComparer
                implementing class.

                --
                Jon Skeet - <skeet@pobox.co m>
                http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                If replying to the group, please do not mail me too

                Comment

                • Chris Dunaway

                  #9
                  Re: Sorting objects, that are in an ArrayList?

                  > Inside that function, is that another User object being sent in? And should[color=blue]
                  > I do something like this?
                  >
                  > User objUserFromOuts ide = (obj as User);
                  > if ( this.FirstName > objUserFromOuts ide.FirstName)
                  > return 1;
                  > else
                  > {
                  > if ( this.FirstName == objUserFromOuts ide.FirstName)
                  > return 0;
                  > else
                  > return -1;
                  > }[/color]

                  Yes, that looks correct.
                  [color=blue]
                  >
                  >
                  > If so - then how would I handle a sort of different fields (like lastname,
                  > address, city, etc)???? Thanks again
                  >[/color]

                  For that, you would probably have to use a class that implements
                  IComparer instead. Then add a constructor to that class that indicates
                  the field to compare against. Something like this (watch for typos):

                  Public Class MyComparer
                  Implements IComparer

                  Private _compareField As String

                  Public Sub New(ByVal comparefield As String)
                  _compareField = comparefield
                  End Sub

                  Public Function Compare(ByVal x As Object, ByVal y As Object) As
                  Integer Implements System.Collecti ons.IComparer.C ompare

                  Select Case _compareField
                  Case "FieldA"
                  'Compare Logic for fieldA
                  Case "FieldB"
                  'Compare Logic for fieldB
                  Case "FieldC"
                  'Compare Logic for fieldC
                  Case Else
                  'Default compare logic
                  End Select

                  End Function
                  End Class


                  Then you could call it like this:

                  Dim al As New ArrayList
                  'populate the arraylist here somehow

                  'Sort on FieldB
                  al.Sort(New MyComparer("Fie ldB"))

                  Comment

                  • Chris Dunaway

                    #10
                    Re: Sorting objects, that are in an ArrayList?

                    Chris Dunaway wrote:
                    [color=blue][color=green]
                    >> <A Big Bunch of VB Code>[/color][/color]

                    I'm sorry, I forgot I was in the CSharp group, but the code is the
                    same.

                    Comment

                    • James Curran

                      #11
                      Re: Sorting objects, that are in an ArrayList?

                      When you implement IComparible (I'm assuming you are the creator of
                      FileObject and can add methods to it), you will wrote a CompareTo() method
                      for the class -- which ArrayList.Sort will call. Essentially(*), somewhere
                      deep inside Sort() are the lines:

                      IComparible left = myArrayList[x] as IComparible;
                      IComparible right = myArrayList[y] as IComparible;
                      if (left.CompareTo (right) > 0 )
                      {
                      // swap myArrayList[x] & myArrayList[y]
                      }

                      (*) I say "essentiall y", because the actual code is so optimized &
                      refactored, those actual lines aren't there at all, but that is in effect
                      what is happening.


                      --
                      Truth,
                      James Curran
                      [erstwhile VC++ MVP]

                      Home: www.noveltheory.com Work: www.njtheater.com
                      Blog: www.honestillusion.com Day Job: www.partsearch.com
                      "RCS" <rseder@gmail.c om> wrote in message
                      news:Iklnf.3798 $nA2.848@newssv r22.news.prodig y.net...[color=blue]
                      > Thank you guys for the response - and I'm sort of with you guys on these
                      > ideas (this was in the ballpark of what I was thinking, but I don't know[/color]
                      how[color=blue]
                      > to implement this). I don't understand *how* I would actually determine[/color]
                      how[color=blue]
                      > to sort my objects. Let me give a more thorough example...
                      >
                      > I have a class called FileObject - which is a class that represents[/color]
                      exactly[color=blue]
                      > one file or directory, it has public get properties for filename, size,
                      > extension, etc, etc.. Now, my other class FileSystemDataO bject, populates[/color]
                      an[color=blue]
                      > arraylist, full of these FileObject objects (one, per file in a[/color]
                      directory),[color=blue]
                      > as it loops through System.IO.Direc tory.GetFiles(@ "C:\") - for example.
                      >
                      > So FileSystemDataO bject has a populated ArrayList - that contains many
                      > objects of type FileObject.
                      >
                      > So, are you saying I should implement IComparable in the FileObject class?
                      > If so - how does that help the containing ArrayList in sorting these
                      > objects? I think I just need one more tiny push to grasp this.. thanks
                      > much..
                      >
                      >
                      > "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                      > news:MPG.1e07db 49789cd23698cb1 c@msnews.micros oft.com...[color=green]
                      > > RCS <rseder@gmail.c om> wrote:[color=darkred]
                      > >> So I have an ArrayList that gets populated with objects like:
                      > >>
                      > >> myAL.Add(new CustomObject(pa rm1,parm2));
                      > >>
                      > >> I'm consuming this ArrayList from an ObjectDataSourc e and would like to
                      > >> have
                      > >> this support sorting (because it's ultimately being consumed in a
                      > >> GridView).
                      > >> I can't simply sort the ArrayList (because it only knows it's holding a
                      > >> bunch of objects). So I need a way to sort the ArrayList, based on the
                      > >> data - that is within the objects that it has loaded.
                      > >>
                      > >> It seems like there must be some relatively simple way that I can make
                      > >> that
                      > >> CustomObject() class sortable... any ideas??? TIA[/color]
                      > >
                      > > You can sort the ArrayList either by implementing IComparable within
                      > > the class itself, or by passing an IComparer which can compare two
                      > > CustomObjects into the call to ArrayList.Sort.
                      > >
                      > > --
                      > > Jon Skeet - <skeet@pobox.co m>
                      > > http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                      > > If replying to the group, please do not mail me too[/color]
                      >
                      >[/color]


                      Comment

                      • Chris Dunaway

                        #12
                        Re: Sorting objects, that are in an ArrayList?

                        Chris Dunaway wrote:[color=blue]
                        > For that, you would probably have to use a class that implements
                        > IComparer instead. Then add a constructor to that class that indicates
                        > the field to compare against. Something like this (watch for typos):[/color]

                        And now that I think about it, you might do it by adding a static
                        property to your class that implements IComparable and do the same
                        thing. It would have to be static otherwise you would have to reset
                        the property on all instances every time you wanted to change the sort.
                        The calling code might be something like this:

                        MyComparableCla ss.SortField = "SomeField" ;
                        al.Sort();

                        Comment

                        • RCS

                          #13
                          Re: Sorting objects, that are in an ArrayList?

                          Chris, John and James,

                          This is just what I needed - now I'm well underway!! this works just about
                          as I wanted it to. Thanks very, very much!!

                          and Chris, here's that code translated into C# and it works like a charm:

                          public class FileObjectCompa rer : IComparer
                          {
                          private string _compareField = "";

                          public FileObjectCompa rer(string comparefield)
                          {
                          _compareField = comparefield;
                          }

                          public int Compare(object x, object y)
                          {
                          switch (_compareField)
                          {
                          case "FileName":
                          {
                          return (x as FileObject).Fil eName.CompareTo ((y as
                          FileObject).Fil eName);
                          break;
                          }
                          case "FileSize":
                          {
                          return (x as FileObject).Fil eSize.CompareTo ((y as
                          FileObject).Fil eSize);
                          break;
                          }
                          default:
                          {
                          return (x as FileObject).Fil eName.CompareTo ((y as
                          FileObject).Fil eName);
                          break;
                          }
                          }
                          }
                          }


                          "Chris Dunaway" <dunawayc@gmail .com> wrote in message
                          news:1134423060 .644346.128390@ g49g2000cwa.goo glegroups.com.. .[color=blue][color=green]
                          >> Inside that function, is that another User object being sent in? And
                          >> should
                          >> I do something like this?
                          >>
                          >> User objUserFromOuts ide = (obj as User);
                          >> if ( this.FirstName > objUserFromOuts ide.FirstName)
                          >> return 1;
                          >> else
                          >> {
                          >> if ( this.FirstName == objUserFromOuts ide.FirstName)
                          >> return 0;
                          >> else
                          >> return -1;
                          >> }[/color]
                          >
                          > Yes, that looks correct.
                          >[color=green]
                          >>
                          >>
                          >> If so - then how would I handle a sort of different fields (like
                          >> lastname,
                          >> address, city, etc)???? Thanks again
                          >>[/color]
                          >
                          > For that, you would probably have to use a class that implements
                          > IComparer instead. Then add a constructor to that class that indicates
                          > the field to compare against. Something like this (watch for typos):
                          >
                          > Public Class MyComparer
                          > Implements IComparer
                          >
                          > Private _compareField As String
                          >
                          > Public Sub New(ByVal comparefield As String)
                          > _compareField = comparefield
                          > End Sub
                          >
                          > Public Function Compare(ByVal x As Object, ByVal y As Object) As
                          > Integer Implements System.Collecti ons.IComparer.C ompare
                          >
                          > Select Case _compareField
                          > Case "FieldA"
                          > 'Compare Logic for fieldA
                          > Case "FieldB"
                          > 'Compare Logic for fieldB
                          > Case "FieldC"
                          > 'Compare Logic for fieldC
                          > Case Else
                          > 'Default compare logic
                          > End Select
                          >
                          > End Function
                          > End Class
                          >
                          >
                          > Then you could call it like this:
                          >
                          > Dim al As New ArrayList
                          > 'populate the arraylist here somehow
                          >
                          > 'Sort on FieldB
                          > al.Sort(New MyComparer("Fie ldB"))
                          >[/color]


                          Comment

                          • Rick Lones

                            #14
                            Re: Sorting objects, that are in an ArrayList?

                            RCS wrote:[color=blue]
                            > So I have an ArrayList that gets populated with objects like:
                            >
                            > myAL.Add(new CustomObject(pa rm1,parm2));
                            >
                            > I'm consuming this ArrayList from an ObjectDataSourc e and would like to have
                            > this support sorting (because it's ultimately being consumed in a GridView).
                            > I can't simply sort the ArrayList (because it only knows it's holding a
                            > bunch of objects). So I need a way to sort the ArrayList, based on the
                            > data - that is within the objects that it has loaded.
                            >
                            > It seems like there must be some relatively simple way that I can make that
                            > CustomObject() class sortable... any ideas??? TIA[/color]

                            Example:

                            public class foo : IComparable
                            {
                            string FirstName;
                            string LastName;
                            . . . etc;

                            public int CompareTo(objec t bar)
                            {
                            int highOrder;
                            if ((highOrder = this.LastName.C ompareTo(bar.La stName)) != 0)
                            return highOrder;
                            else return this.FirstName. CompareTo(bar.F irstName);
                            }
                            }

                            If you have instances of foo, they can now be added as the key component of,
                            e.g., a SortedList and would (in this example) cause the list to sort in
                            (LastName, FirstName) order. The apparent recursion bottoms out right away
                            because string are natively comparable. You can extend this to create composite
                            indices of any type(s) you want as long as you can either define an ordering or
                            make use of a "natural" ordering for the type at hand.

                            HTH,
                            -rick-

                            Comment

                            • John Vottero

                              #15
                              Re: Sorting objects, that are in an ArrayList?

                              You can take this a step further and create a Comparer class that uses
                              reflection to look at the properties of the object it's comparing to do the
                              compare. Then you get rid of the switch statement and use the same comparer
                              class for any object. Here's an example:

                              /// <summary>
                              /// The PropComparer class is a generic comparer. It takes a
                              PropertyDescrip tor
                              /// and compares two of whatever type the descriptor points to.
                              /// </summary>
                              public class PropComparer : IComparer
                              {
                              private PropertyDescrip tor sortProperty;
                              private ListSortDirecti on sortDirection;

                              /// <summary>
                              /// Creates a new PropComparer that will use the specified property
                              and direction.
                              /// </summary>
                              /// <param name="initPrope rty">The property to use.</param>
                              /// <param name="initDirec tion">The sort direction to use.</param>
                              public PropComparer(Pr opertyDescripto r initProperty,
                              ListSortDirecti on initDirection)
                              {
                              sortProperty = initProperty;
                              sortDirection = initDirection;
                              }

                              /// <summary>
                              /// Compares two objects based upon the passed properties.
                              /// </summary>
                              /// <param name="x">The property of object X.</param>
                              /// <param name="y">The property of object Y.</param>
                              /// <returns></returns>
                              public int Compare(object x, object y)
                              {
                              int result;
                              IComparable cmpIf;
                              object propertyX;
                              object propertyY;

                              //
                              // Get the actual objects that we are comparing
                              // These a basically fields from the X and Y objects
                              //
                              propertyX = sortProperty.Ge tValue(x);
                              propertyY = sortProperty.Ge tValue(y);

                              //
                              // Deal with one or both being null
                              //
                              if ((propertyX == null) && (propertyY == null))
                              {
                              result = 0;
                              }
                              else if (propertyX == null)
                              {
                              result = -1;
                              }
                              else if (propertyY == null)
                              {
                              result = 1;
                              }
                              else
                              {
                              //
                              // Get the IComparable interface
                              // This will throw an exception if the object isn't
                              comparable
                              //
                              cmpIf = (IComparable)pr opertyX;

                              //
                              // Compare X.Property and Y.Property
                              //
                              result = cmpIf.CompareTo (propertyY);
                              }

                              //
                              // If they are equal, see if we can compare the parent objects
                              //
                              if (result == 0)
                              {
                              //
                              // Get the IComparable interface from the X object
                              // This won't throw an exception if IComparable isn't
                              supported
                              //
                              cmpIf = x as IComparable;

                              if (cmpIf != null)
                              {
                              //
                              // Compare X and Y
                              //
                              result = cmpIf.CompareTo (y);
                              }
                              }

                              //
                              // If the direction is descending, reverse the sign
                              //
                              if (sortDirection == ListSortDirecti on.Descending)
                              {
                              result = -result;
                              }

                              return result;
                              }
                              }



                              and here is how you would use the PropComparer:

                              //
                              // Get a collection of property descriptors for your object
                              //
                              PropertyDescrip torCollection pdc =
                              TypeDescriptor. GetProperties(t ypeof(YourClass ));

                              //
                              // Sort your list with the PropComparer
                              //
                              TheList.Sort(ne w PropComparer(pd c["FirstName"],
                              ListSortDirecti on.Ascending));


                              Reflection is one of the coolest features of .NET.

                              John Vottero



                              "RCS" <rseder@gmail.c om> wrote in message
                              news:S4pnf.3728 $fO5.1463@newss vr33.news.prodi gy.com...[color=blue]
                              > Chris, John and James,
                              >
                              > This is just what I needed - now I'm well underway!! this works just about
                              > as I wanted it to. Thanks very, very much!!
                              >
                              > and Chris, here's that code translated into C# and it works like a charm:
                              >
                              > public class FileObjectCompa rer : IComparer
                              > {
                              > private string _compareField = "";
                              >
                              > public FileObjectCompa rer(string comparefield)
                              > {
                              > _compareField = comparefield;
                              > }
                              >
                              > public int Compare(object x, object y)
                              > {
                              > switch (_compareField)
                              > {
                              > case "FileName":
                              > {
                              > return (x as FileObject).Fil eName.CompareTo ((y as
                              > FileObject).Fil eName);
                              > break;
                              > }
                              > case "FileSize":
                              > {
                              > return (x as FileObject).Fil eSize.CompareTo ((y as
                              > FileObject).Fil eSize);
                              > break;
                              > }
                              > default:
                              > {
                              > return (x as FileObject).Fil eName.CompareTo ((y as
                              > FileObject).Fil eName);
                              > break;
                              > }
                              > }
                              > }
                              > }
                              >
                              >
                              > "Chris Dunaway" <dunawayc@gmail .com> wrote in message
                              > news:1134423060 .644346.128390@ g49g2000cwa.goo glegroups.com.. .[color=green][color=darkred]
                              >>> Inside that function, is that another User object being sent in? And
                              >>> should
                              >>> I do something like this?
                              >>>
                              >>> User objUserFromOuts ide = (obj as User);
                              >>> if ( this.FirstName > objUserFromOuts ide.FirstName)
                              >>> return 1;
                              >>> else
                              >>> {
                              >>> if ( this.FirstName == objUserFromOuts ide.FirstName)
                              >>> return 0;
                              >>> else
                              >>> return -1;
                              >>> }[/color]
                              >>
                              >> Yes, that looks correct.
                              >>[color=darkred]
                              >>>
                              >>>
                              >>> If so - then how would I handle a sort of different fields (like
                              >>> lastname,
                              >>> address, city, etc)???? Thanks again
                              >>>[/color]
                              >>
                              >> For that, you would probably have to use a class that implements
                              >> IComparer instead. Then add a constructor to that class that indicates
                              >> the field to compare against. Something like this (watch for typos):
                              >>
                              >> Public Class MyComparer
                              >> Implements IComparer
                              >>
                              >> Private _compareField As String
                              >>
                              >> Public Sub New(ByVal comparefield As String)
                              >> _compareField = comparefield
                              >> End Sub
                              >>
                              >> Public Function Compare(ByVal x As Object, ByVal y As Object) As
                              >> Integer Implements System.Collecti ons.IComparer.C ompare
                              >>
                              >> Select Case _compareField
                              >> Case "FieldA"
                              >> 'Compare Logic for fieldA
                              >> Case "FieldB"
                              >> 'Compare Logic for fieldB
                              >> Case "FieldC"
                              >> 'Compare Logic for fieldC
                              >> Case Else
                              >> 'Default compare logic
                              >> End Select
                              >>
                              >> End Function
                              >> End Class
                              >>
                              >>
                              >> Then you could call it like this:
                              >>
                              >> Dim al As New ArrayList
                              >> 'populate the arraylist here somehow
                              >>
                              >> 'Sort on FieldB
                              >> al.Sort(New MyComparer("Fie ldB"))
                              >>[/color]
                              >
                              >[/color]


                              Comment

                              Working...