Solved

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

Posted on 2010-08-16
3
473 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 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Asp.Net Session Question 2 36
global Variable - 2 functions in powershell 1 22
Selenium and Xpath 4 35
Adjust the codes 3 37
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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

726 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