Solved

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

Posted on 2010-08-16
3
469 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
Comment Utility

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
Comment Utility
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
Comment Utility
   
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 Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now