tech1guy
asked on
Sort both ways Arraylist of objects
Hello Experts,
I'm a beginer in C#.
I have an arrayList containing LastName, Firstname
What I am trying to achieve is sort both ways (ascending and descending order) on both first name and lastname. Something like: Sort(sortField, sortDirection)
Is there a way to do this in C# ?
Thanks in advance
I'm a beginer in C#.
I have an arrayList containing LastName, Firstname
What I am trying to achieve is sort both ways (ascending and descending order) on both first name and lastname. Something like: Sort(sortField, sortDirection)
Is there a way to do this in C# ?
Thanks in advance
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi
You can then use the ArrayList.Sort() method - if your business object implements a System.Collection.ICompare r method, you can use that by default by calling Sort() with no parameters, or you can create a new instance of a custom IComparer - the following code demonstrates sorting the product versions loaded in the previous step by name (the ArrayList.Sort() documentation can provide a more extensive example of how to implement a default IComparer in your business object - this example uses external IComparer implementations):
versions.Sort(new ProductVersionNameSortAsce nding());
// or
versions.Sort(new ProductVersionNameSortDesc ending());
// sorts ascending by title:
public class ProductVersionNameSortAsce nding : System.Collections.ICompar er
{
public int Compare(object x, object y)
{
return ((Version)x)._title.Compar eTo(((Vers ion)y)._ti tle);
}
}
// sorts descending by title:
public class ProductVersionNameSortDesc ending : System.Collections.ICompar er
{
public int Compare(object x, object y)
{
int i = ((Version)x)._title.Compar eTo(((Vers ion)y)._ti tle);
return (i * -1);
}
}
BTW... I got this from http://mongoosesolutions.com/mg/forum_posts.aspx?thread_id=771
You can then use the ArrayList.Sort() method - if your business object implements a System.Collection.ICompare
versions.Sort(new ProductVersionNameSortAsce
// or
versions.Sort(new ProductVersionNameSortDesc
// sorts ascending by title:
public class ProductVersionNameSortAsce
{
public int Compare(object x, object y)
{
return ((Version)x)._title.Compar
}
}
// sorts descending by title:
public class ProductVersionNameSortDesc
{
public int Compare(object x, object y)
{
int i = ((Version)x)._title.Compar
return (i * -1);
}
}
BTW... I got this from http://mongoosesolutions.com/mg/forum_posts.aspx?thread_id=771
guys all that is covered in detail in the link i posted.
gp
gp
The msdn examples in the link posted doesn't show how to sort an ArrayList based on its elements' substrings ("lastname, firstname"), which I think is what tech1guy is having difficulty with...
ASKER
Hi everybody,
Thanks for everyone's response. I think that Zephyr__ knows where I'm stuck. We are near the solution. I've gone through your solution, is there any way that I can get rid of permutation nad combinations for ascending and descending like (if first && ascending order OR firstname && descending order)............... something like: generic kind of thing for any number of columns........
Thanks for everyone's response. I think that Zephyr__ knows where I'm stuck. We are near the solution. I've gone through your solution, is there any way that I can get rid of permutation nad combinations for ascending and descending like (if first && ascending order OR firstname && descending order)............... something like: generic kind of thing for any number of columns........
ASKER
Hi Zephyr__ ,
My arraylist does NOT contain a strings 'firstname, lastname'..............act ually, my arralist contains the objects as columns: like 2 columns (firstname and lastname) and then each column is having their values as follows:
Firstname Lastname
Smith Don
John Stepson
Ron Gill
So, you can say that it's ArrayList of Objects. Actually I'm specifying this ArrayList as Datasource for my DataGrid.
My arraylist does NOT contain a strings 'firstname, lastname'..............act
Firstname Lastname
Smith Don
John Stepson
Ron Gill
So, you can say that it's ArrayList of Objects. Actually I'm specifying this ArrayList as Datasource for my DataGrid.
Hi, could you be a little clearer on what type your storing in the ArrayList, do you mean your storing DataColumn objects?
ASKER
Basically, I'm generating arraylist from database (which is already in business code).......all I've is ArrayList and I'm binding my arraylist to datagrid with two column as firstname and lastname.................. ..it's going fine................the problem is that I am trying to sort the datagrid by clicking the datagrid column heading..............As everything is generated at business end............so, I'm unable to re-execute the sql command..............So, I'm left with the option to sort my ArrayList (which contains Firstname, Lastname and their corresponding values)................... I hope this will be helpful to you in solving my problem.
If your generating the values from a database why put them into an ArrayList at all? You have all the sorting capablities you need inside of a DataSet/DataTable/DataView . Check out this piece of code.
public void BindGrid(String sortfield)
{
// Set up the SqlDataAdapter to get the "Authors" table from the
// "pubs" database.
SqlDataAdapter myCommand = new SqlDataAdapter("SELECT * FROM " +
"Authors", myConnection);
// Fill a new DataSet.
DataSet ds = new DataSet();
myCommand.Fill(ds);
// Create a DataView based on the first (and only) DataTable in the
// DataSet. Sort the data using the specified sort field.
DataView view = ds.Tables[0].DefaultView;
// To sort in ascending order:
view.Sort = sortfield + " ASC";
// To sort in descending order:
// view.Sort = sortfield + " DESC";
// Bind the DataGrid to the sorted DataView.
MyDataGrid.DataSource=view ;
MyDataGrid.DataBind();
}
Another good way to go about it is using the DataTable.Select() method.
gp
public void BindGrid(String sortfield)
{
// Set up the SqlDataAdapter to get the "Authors" table from the
// "pubs" database.
SqlDataAdapter myCommand = new SqlDataAdapter("SELECT * FROM " +
"Authors", myConnection);
// Fill a new DataSet.
DataSet ds = new DataSet();
myCommand.Fill(ds);
// Create a DataView based on the first (and only) DataTable in the
// DataSet. Sort the data using the specified sort field.
DataView view = ds.Tables[0].DefaultView;
// To sort in ascending order:
view.Sort = sortfield + " ASC";
// To sort in descending order:
// view.Sort = sortfield + " DESC";
// Bind the DataGrid to the sorted DataView.
MyDataGrid.DataSource=view
MyDataGrid.DataBind();
}
Another good way to go about it is using the DataTable.Select() method.
gp
ASKER
As told by my seniors that in pure OOP and layered design, we have to pass ONLY arraylist to the presentation layer.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemCollectionsArrayListClassSortTopic.asp
The point here is you will need to impletment IComparer so that the arraylist knows "how" to sort the items you want. Then you will call its sort method and choose ascending/descending order. That link shows the very same thing you asking, sorting names.
gp