You must Sign In to post a response.
  • Category: .NET

    how to add structure-elements to a vector n sort it dependinn on a variable count in that structure?

    The forum thread has not been reviewed by Editors yet. Readers are advised to use their best judgement before accessing this thread.
    This thread will be reviewed shortly.
    If you think this thread contain inappropriate content, please report to webmaster.
    i have n no. of scemes with respective count
    so i have created a structure which has variables as int count & string scheme_name.
    i want to sort the schemes depending on the value of count of respective schemes.
    how can i do it?

    is it possible to create a vector, then add all the structures in it & then to sort the vector dependinn on the value of the count??
  • #224
    Try this !,

    You can use the IComparer interface method to sort the contents. Im not able to figure out what exactly the vector which you are mentioning, Im assuming it to be a collection , so using ArrayList for storing and sorting.

    <B>Sample struct.</B>

    <font style="FONT-SIZE: 11px; color:blue;FONT-FAMILY: 'Courier New'" >

    public struct Schemes
    {
    public int count;
    public string schema;
    }

    </font>

    <B>Code for adding the structure to the collection and sorting</B>

    <font style="FONT-SIZE: 11px; color:blue;FONT-FAMILY: 'Courier New'" >

    Public void AddAndSort()
    {

    ArrayList myList = new ArrayList();

    Schemes s1 = new Schemes();
    s1.count = 1;
    s1.schema = "Schema1";

    Schemes s2 = new Schemes();
    s2.count = 2;
    s2.schema = "Schema2";

    Schemes s3 = new Schemes();
    s3.count = 3;
    s3.schema = "Schema3";

    Schemes s4 = new Schemes();
    s4.count = 4;
    s4.schema = "Schema4";

    Schemes s5 = new Schemes();
    s5.count = 5;
    s5.schema = "Schema5";

    // adding the struct objects to the collection randomly

    myList.Add(s3);
    myList.Add(s5);
    myList.Add(s4);
    myList.Add(s1);
    myList.Add(s2);

    myList.Sort(this);

    foreach(Schemes s in myList)
    {
    Response.Write(s.schema);
    Response.Write("<BR>");
    }

    }
    </font>

    In the above method, we are calling myList.Sort(this) , this method of ArrayList
    will take the reference of IComparer interface, so we have to implement the
    IComparer interface in the code behind (If ur using ASP.NET,else in the windows
    form class). Below given is the implementationf for the IComparer.Compare method.

    <font style="FONT-SIZE: 11px; color:blue;FONT-FAMILY: 'Courier New'" >

    public class WebForm2 : System.Web.UI.Page,IComparer
    {

    public int Compare(object o1,object o2)
    {

    Schemes s1 = (Schemes)o1;
    Schemes s2 = (Schemes)o2;

    if(s1.count < s2.count)
    return -1;
    else if(s1.count == s2.count)
    return 0;
    else
    return 1;
    }

    }

    </font>

    Hope this will help you.

    Cheers!
    John


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.