Click here to Skip to main content
Licence Public Domain
First Posted 21 May 2009
Views 6,262
Downloads 44
Bookmarked 9 times

SequenceCompare - Comparing Sequences of IEnumerable

By Phil Martin | 21 May 2009
A simple set of extension methods comparing sequences of IEnumerable

1

2

3
1 vote, 100.0%
4

5
4.00/5 - 1 vote
μ 4.00, σa 5.00 [?]
Sample Image

Introduction

This short article describes a simple extension method for comparing sequences of values, and therefore sorting sequences against each other. 

The original need for these extension methods was that we were displaying a series of lists in grid cells, and we wanted to be able to sort the grid by that column. Ordinary alphabetical sorting on the display text was inadequate because if some elements were numbers, then alphabetical falls short of being useful. Using natural sorting on the display text was an option, but this was a simpler route.

What about Enumerable.SequenceEqual()?

While this code does resemble SequenceEqual, it's intended use is quite different. Since this provides comparison and not just equality, it enables you to sort sequences of sequences, rather than just testing if two sequencces are the same or not.

Using the Code

To use the code, simply include:

using SequenceCompareLib;

Or copy the code to your own project and modify the namespace.

After this, you can:

  • Call SequenceCompare<T> on any IEnumerable<T> where T implements IComparable<T>
  • Call List<IEnumerable<T>>.Sort() passing in SequenceComparer<T>.Default
  • Also specify your own IComparer<T> instance to do the element comparisons

For example:

var random = new Random();
var values = new List<IEnumerable<int>>();

for (int i = 0; i < 10; ++i)
{
  var items = new List<int>();
  var count = random.Next(0, 6);
  for (int j = 0; j < count; ++j)
  {
    items.Add(random.Next(5));
  }

  items.Sort();
  values.Add(items);
}

values.Sort(SequenceComparer<int>.Default);

This will produce something like this:

0 
0 1 
1 1 2 2 3 
1 2 
1 2 
1 3 
1 3 
3 
3 3 

History

  • 21st May, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication

About the Author

Phil Martin

Software Developer (Senior)
MineSched Software International
Canada Canada

Member
A professional developer in the Mining sector since 2001, Phil has a passion for creating robust, quality scientific applications.
 
My recent professional work focuses on scheduling and optimisation in the mining sector.
 
My major fields of interest in software are 3D applications, visualisation, scheduling and optimisation, and novel UI design. My goal as a professional developer is to help people solve real world problems through reliable and fun to use software.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMust implement call IDisposable.Dispose on enumerators. PinmemberMarc Brooks14:13 26 May '09  
GeneralRe: Must implement call IDisposable.Dispose on enumerators. PinmemberPhil Martin...17:24 26 May '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120210.1 | Last Updated 21 May 2009
Article Copyright 2009 by Phil Martin
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid