Solved

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

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

688 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