[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

How do I sort items within an ArrayList or GenericList using custom compare and sort criteria?

Posted on 2010-08-16
3
Medium Priority
?
481 Views
Last Modified: 2013-11-08
I wanted to find out if there is a way to sort an ArrayList or Generic List (either will work) by different criteria within the same list using either Linq or ICompare type code.

Example:  Imagine that someone wants to display a list of people in a sort order that can take two criteria into account when doing the sort, like an explicit integer based sort order then a default fall-back order for any items that do not have an explicit ordering.

If you have a list of these three people:
      John Doe (Name = John Doe, Result Count = 5, Sort Order = 2)
      Jane Doe (Name = Jane Doe, Result Count = 75, Sort Order = 3)
      Jimmy Joe (Name = Jimmy Joe, Result Count = 15, Sort Order = 1)
      Jolene Joe (Name = Jolene Joe, Result Count = 10, Sort Order = 0)


How could you define a sort on the list (using either an arraylist or generic list) that can do the following?

      Sort the list first by Sort Order (in ascending)
            Then if the Sort Order of an item is 0 or null (assume it can be a Nullable integer), sort the items that fall into this category under the others using an Alphabetical sort (in ascending order)

Or

      Sort the list first by Sort Order (in ascending)
            Then if the Sort Order of an item is 0 or null (assume it can be a Nullable integer), sort the items that fall into this category under the others using a sort of Result Count (in descending order)



Class Example

Person Class
Name (String)
ResultCount (Int)
SortOrder(Int)
0
Comment
Question by:endrec
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 2

Expert Comment

by:Algol36
ID: 33447356

class Person
{
   public string Name;
   public int ResultCount;
   public int SortOrder;
}

class PersonComparer : IComparer<Person>
{
   public int Compare(Person x, Person y)
   {
      if (x.SortOrder == 0 && y.SortOrder == 0)
         return x.Name.CompareTo(y.Name);
      if (x.SortOrder == 0)
         return 1;
      if (y.SortOrder == 0)
         return -1;
      return x.SortOrder.CompareTo(y.SortOrder);
   }
}  
....
List<Person> list = new List<Person>();
list.Add(new Person() { Name = "zzz", SortOrder = 0 });
list.Add(new Person() { Name = "bbb", SortOrder = 1 });
list.Add(new Person() { Name = "ccc", SortOrder = 2 });
list.Add(new Person() { Name = "xxx", SortOrder = 0 });
list.Sort(new PersonComparer());
0
 

Author Comment

by:endrec
ID: 33447573
How do you add a descending sort on ResultCount into the comparer, as the other sorts as acending?
0
 
LVL 2

Accepted Solution

by:
Algol36 earned 2000 total points
ID: 33447647
   
class PersonComparer : IComparer<Person>
{
   public int Compare(Person x, Person y)
   {
      if (x.SortOrder == 0 && y.SortOrder == 0)
      {
         //first compare by ResultCount
         int res = - x.ResultCount.CompareTo(y.ResultCount);
         if (res != 0)
            return res;
         //second compare by Name
         return x.Name.CompareTo(y.Name);
      }
      if (x.SortOrder == 0)
         return 1;
      if (y.SortOrder == 0)
         return -1;
      return x.SortOrder.CompareTo(y.SortOrder);
   }
}    
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question