Solved

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

Posted on 2010-08-16
3
472 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
  • 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 500 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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

839 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