Files
spark/src/DataStreamSuite/DataStreamCore/BlueData/BlueRowComparer.cs
T
2023-09-04 16:25:40 -04:00

61 lines
1.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace BCG
{
/// <summary>
/// Generic IComparer for IBlueRow. Accepts a list of column names to define the sort order. All included columns must be IComparable.
/// </summary>
public class BlueRowComparer : IComparer<IBlueRow>, IComparer
{
private IEnumerable<string> myCompareColumns;
private bool myIsAscending;
public BlueRowComparer(bool ascending, params string[] columns)
{
this.myCompareColumns = columns;
this.myIsAscending = ascending;
}
public BlueRowComparer(params string[] columns)
: this(true, columns)
{
}
public int Compare(IBlueRow x, IBlueRow y)
{
// Guard against nulls
if (x == y) return 0;
if (x == null) return -1;
if (y == null) return 1;
int result = 0;
foreach (string compareColumn in this.myCompareColumns)
{
object left = x[compareColumn];
object right = y[compareColumn];
if (left == right) result = 0;
else if (left == null || left == DBNull.Value) result = -1;
else if (right == null || right == DBNull.Value) result = 1;
else result = ((IComparable)left).CompareTo(right);
if (result != 0)
break;
}
return result * (this.myIsAscending ? 1 : -1);
}
#region IComparer Members
public int Compare(object x, object y)
{
return this.Compare(x as IBlueRow, y as IBlueRow);
}
#endregion
}
}