Sorting an array list containing objects?

Discussion in 'ASP .Net' started by Rob Meade, Jan 10, 2006.

  1. Rob Meade

    Rob Meade Guest

    Dear all,

    I have a class which contains an arraylist populated with other objects, for
    example:

    PrescriptionQueue - containing multiple instances of Prescription

    I have the need on my web page to display this data which I have done,
    however, now I would like to sort it based on a data item/direction selected
    by the user from the web page.

    I have separate data and display layers and was planning on doing my "sort"
    in my display layer.

    In order to do this I figured I would need to pull the Prescription objects
    back out from my PrescriptionQueue, perhaps pop them into a DataTable/View
    to make the sorting easier.

    I then wondered whether it would be better to try and sort the
    PrescriptionQueue itself.

    Got a little lost after this point :eek:)

    From what I've seen of the ArrayList.Sort you can do this quite easily:

    ArrayList.Add("Red")
    ArrayList.Add("Blue")
    ArrayList.Add("Green")

    ArrayList.Sort()

    etc, but thats based on the value directly inside those elements of the
    arraylist, is it possible to do something more like this:

    For Each Prescription In PrsecriptionQueue

    ArrayList.Add(Prescription)

    Next

    ArrayList.Sort(Prescription.PatientSurname)

    I appreciate the above example is probably not syntactically correct, so
    please excuse me, I am merely trying to give an example of what I would like
    to achieve.

    If anyone has any thoughts/suggestions as to doing this a better way I would
    be most grateful - another idea I had was requesting the sort order when I
    "get" my data in the first place.

    Any help appreciated,

    Regards

    Rob
    PS: Happy New Year!
    Rob Meade, Jan 10, 2006
    #1
    1. Advertising

  2. Rob Meade

    Mantorok Guest

    Look at the static method Array.Sort, you will have to break up your array
    into 2 - one for keys and one for the corresponding values.

    It's not particularly trivial but you can write your own method to deal with
    it.

    Kev

    "Rob Meade" <> wrote in message
    news:%23o$...
    > Dear all,
    >
    > I have a class which contains an arraylist populated with other objects,
    > for example:
    >
    > PrescriptionQueue - containing multiple instances of Prescription
    >
    > I have the need on my web page to display this data which I have done,
    > however, now I would like to sort it based on a data item/direction
    > selected by the user from the web page.
    >
    > I have separate data and display layers and was planning on doing my
    > "sort" in my display layer.
    >
    > In order to do this I figured I would need to pull the Prescription
    > objects back out from my PrescriptionQueue, perhaps pop them into a
    > DataTable/View to make the sorting easier.
    >
    > I then wondered whether it would be better to try and sort the
    > PrescriptionQueue itself.
    >
    > Got a little lost after this point :eek:)
    >
    > From what I've seen of the ArrayList.Sort you can do this quite easily:
    >
    > ArrayList.Add("Red")
    > ArrayList.Add("Blue")
    > ArrayList.Add("Green")
    >
    > ArrayList.Sort()
    >
    > etc, but thats based on the value directly inside those elements of the
    > arraylist, is it possible to do something more like this:
    >
    > For Each Prescription In PrsecriptionQueue
    >
    > ArrayList.Add(Prescription)
    >
    > Next
    >
    > ArrayList.Sort(Prescription.PatientSurname)
    >
    > I appreciate the above example is probably not syntactically correct, so
    > please excuse me, I am merely trying to give an example of what I would
    > like to achieve.
    >
    > If anyone has any thoughts/suggestions as to doing this a better way I
    > would be most grateful - another idea I had was requesting the sort order
    > when I "get" my data in the first place.
    >
    > Any help appreciated,
    >
    > Regards
    >
    > Rob
    > PS: Happy New Year!
    >
    >
    Mantorok, Jan 10, 2006
    #2
    1. Advertising

  3. Rob Meade

    Karl Seguin Guest

    ..Sort accepts an IComparer that tells it how to compare.

    arr.Sort(ctype(new UserComparer(), IComparer))

    public class UserComparer
    implements IComparer

    public function Compare(x as object, y as object) as integer implements
    IComparer.Compare
    return string.Compare(ctype(x, user).Name, ctype(y, user).Name)
    end function

    end class


    Take a look at:
    http://msdn.microsoft.com/library/d...systemcollectionsarraylistclasssorttopic2.asp

    Karl
    --
    MY ASP.Net tutorials
    http://www.openmymind.net/


    "Rob Meade" <> wrote in message
    news:%23o$...
    > Dear all,
    >
    > I have a class which contains an arraylist populated with other objects,
    > for example:
    >
    > PrescriptionQueue - containing multiple instances of Prescription
    >
    > I have the need on my web page to display this data which I have done,
    > however, now I would like to sort it based on a data item/direction
    > selected by the user from the web page.
    >
    > I have separate data and display layers and was planning on doing my
    > "sort" in my display layer.
    >
    > In order to do this I figured I would need to pull the Prescription
    > objects back out from my PrescriptionQueue, perhaps pop them into a
    > DataTable/View to make the sorting easier.
    >
    > I then wondered whether it would be better to try and sort the
    > PrescriptionQueue itself.
    >
    > Got a little lost after this point :eek:)
    >
    > From what I've seen of the ArrayList.Sort you can do this quite easily:
    >
    > ArrayList.Add("Red")
    > ArrayList.Add("Blue")
    > ArrayList.Add("Green")
    >
    > ArrayList.Sort()
    >
    > etc, but thats based on the value directly inside those elements of the
    > arraylist, is it possible to do something more like this:
    >
    > For Each Prescription In PrsecriptionQueue
    >
    > ArrayList.Add(Prescription)
    >
    > Next
    >
    > ArrayList.Sort(Prescription.PatientSurname)
    >
    > I appreciate the above example is probably not syntactically correct, so
    > please excuse me, I am merely trying to give an example of what I would
    > like to achieve.
    >
    > If anyone has any thoughts/suggestions as to doing this a better way I
    > would be most grateful - another idea I had was requesting the sort order
    > when I "get" my data in the first place.
    >
    > Any help appreciated,
    >
    > Regards
    >
    > Rob
    > PS: Happy New Year!
    >
    >
    Karl Seguin, Jan 10, 2006
    #3
  4. Rob Meade

    sarangasl

    Joined:
    Oct 14, 2009
    Messages:
    1
    Sort Objects in ArrayList, it is simple.

    Nice !!!!!!!!

    Try this too,

    sarangasl.blogspot.com/2009/10/sort-object-arraylist-in-c.html
    sarangasl, Oct 14, 2009
    #4
  5. Rob Meade

    rahul891

    Joined:
    Nov 4, 2012
    Messages:
    1
    Sorting of ArrayList


    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;

    public class Person implements Comparable{
    String Last_Name=null;
    String First_Name=null;
    public Person(String F,String L){
    this.First_Name=F;
    this.Last_Name=L;
    }

    public String getFirst_Name() {
    return First_Name;
    }

    public void setFirst_Name(String first_Name) {
    First_Name = first_Name;
    }

    public String getLast_Name() {
    return Last_Name;
    }

    public void setLast_Name(String last_Name) {
    Last_Name = last_Name;
    }

    @Override
    public String toString() {
    return Last_Name+", "+ First_Name+";";
    }

    public int compareTo(Object obj) {
    // TODO Auto-generated method stub
    Person person= (Person) obj;
    int compareQuantity = Last_Name.compareTo( person.getLast_Name());

    //ascending order
    return compareQuantity;

    }

    public boolean equals(Object obj) {
    if (!(obj instanceof Person)) {
    return false;
    }
    Person emp = (Person) obj;
    return First_Name.equals(emp.getFirst_Name())
    && Last_Name.equals(emp.getLast_Name());
    }

    public static void main(String arg[]){
    ArrayList al=new ArrayList();
    al.add(new Person("rom","couch"));
    al.add(new Person("james","boucher"));

    System.out.println("al is --->"+al);
    Collections.sort(al);
    System.out.println("al is --->"+al);

    }

    }


    class PersonComparator implements Comparator{
    public int compare(Object obj1, Object obj2) {
    Person emp1 = (Person) obj1;
    Person emp2 = (Person) obj2;

    int nameComp = emp1.getLast_Name().compareTo(emp2.getLast_Name());

    return ((nameComp == 0) ?
    emp1.getFirst_Name().compareTo(emp2.getFirst_Name()) :
    nameComp);
    }
    }
    Last edited: Nov 4, 2012
    rahul891, Nov 4, 2012
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Koen
    Replies:
    1
    Views:
    487
  2. Ronny Mandal

    Sorting of list containing tuples

    Ronny Mandal, May 18, 2006, in forum: Python
    Replies:
    7
    Views:
    319
    Christoph Haas
    May 18, 2006
  3. ALiX
    Replies:
    4
    Views:
    409
    Roland Pibinger
    Feb 15, 2006
  4. markspace
    Replies:
    1
    Views:
    370
    markspace
    Jun 25, 2009
  5. Steve

    Objects containing objects

    Steve, Jan 13, 2008, in forum: Perl Misc
    Replies:
    2
    Views:
    66
    Paul Lalli
    Jan 13, 2008
Loading...

Share This Page