Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C# sorting arraylist by custom object

Options
  • 21-05-2007 8:46pm
    #1
    Registered Users Posts: 872 ✭✭✭


    Hi,

    Im using a google suggest like textbox in an application im making. The example is hooked up to an access database and the results are returned in an arraylist.

    Im doing the same thing but with an xml file. When the contents get read into the array list they get inserted as an object.
    ArrayList a = new ArrayList();
    
    			XmlDocument xDoc = new XmlDocument();
    			xDoc.Load(xml);
    
    			XmlNodeList nodes = xDoc.SelectNodes("establishments/establishment");
    
    			ASBMenuItem oMenuItem;
    
    			foreach (XmlNode node in nodes)
    			{
    
    				[B]oMenuItem[/B]=new ASBMenuItem();
    				oMenuItem.Label = node.Attributes["name"].InnerText;
    				oMenuItem.Value = node.Attributes["id"].InnerText;
    				
    				a.Add([B]oMenuItem[/B]);
    				
    			}
    

    I thought i could just go a.sort() but it says something about a class implementing the IComparer interface.

    Any help is appreciated...


Comments

  • Registered Users Posts: 4,276 ✭✭✭damnyanks


    Yup, you cant really sort a datastructure which .net doesnt know about. You can sort it using icomparer / icomparable or rather you can program to the interface to do some sorting.

    What sort of values are they you are trying to sort/


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    The sort method can only sort objects which inherit from IComparable (as said above).

    For example if you're object had a string called "Name" and you want to be able to sort by that, you could implement the CompareMethod to do this:

    public int CompareTo(object other)
    {
    MyClass blah = (MyClass)other;
    return this.Name.CompareTo(blah.Name);
    }


  • Registered Users Posts: 872 ✭✭✭grahamor


    I decided to forget about the xml and have the data running from the DB instead. The major advantage to this is that no sorting needs to be done. The sproc that im using is like
    SELECT name from table where name LIKE @keyword + '%'
    
    ORDER BY name ASC
    

    The order by clause takes care of the sorting.

    One problem though is the initial DB connection time when someone types in 1 letter into the textbox. Is it possible to establish the DB connection when the page loads or would this waste resources ?

    I using a sqldatareader to get data from DB and then putting it into an arraylist
    ArrayList a = new ArrayList();
    
    			ASBMenuItem oMenuItem;
    
    			SqlDataReader sr = common.getEstablishmentsByName(sKeyword);
    
    			while(sr.Read())
    			{
    				if(sr.HasRows)
    				{
    					oMenuItem=new ASBMenuItem();
    					oMenuItem.Label = sr["name"].ToString();
    					oMenuItem.Value = sr["establishmentID"].ToString();
    				
    					a.Add(oMenuItem);
    				}
    			}
    
    			sr.Close();
    			sr = null;
    
    			return a;
    

    How can i speed up the initial DB Connection ?


  • Registered Users Posts: 4,276 ✭✭✭damnyanks


    Cache the data, store it in a dataset and query the dataset, use a efficient SQL command etc. etc.


Advertisement