c748652c34
commit 8cec8f150da7583b7af5efbe2853efee0179750c
90 lines
2.8 KiB
C#
90 lines
2.8 KiB
C#
/********************************************************************++
|
|
Copyright (c) Microsoft Corporation. All rights reserved.
|
|
--********************************************************************/
|
|
|
|
using System.Management.Automation;
|
|
|
|
namespace Microsoft.PowerShell.Commands
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
[Cmdlet("Sort", "Object", HelpUri = "http://go.microsoft.com/fwlink/?LinkID=113403", RemotingCapability = RemotingCapability.None)]
|
|
public sealed class SortObjectCommand : OrderObjectBase
|
|
{
|
|
#region Command Line Switches
|
|
/// <summary>
|
|
/// This param specifies if sort order is ascending.
|
|
/// </summary>
|
|
[Parameter]
|
|
public SwitchParameter Descending
|
|
{
|
|
get { return DescendingOrder; }
|
|
set { DescendingOrder = value; }
|
|
}
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <value></value>
|
|
[Parameter]
|
|
public SwitchParameter Unique
|
|
{
|
|
get { return unique; }
|
|
set { unique = value; }
|
|
}
|
|
private bool unique;
|
|
#endregion
|
|
|
|
|
|
/// <summary>
|
|
/// Remove Duplicated from <paramref name="sortedList"/>
|
|
/// </summary>
|
|
private static void RemoveDuplicates(OrderByProperty orderByProperty)
|
|
{
|
|
int current = 0, lookAhead;
|
|
OrderByPropertyEntry currentObj = orderByProperty.OrderMatrix[current];
|
|
while (current + 1 < orderByProperty.OrderMatrix.Count)
|
|
{
|
|
lookAhead = current + 1;
|
|
OrderByPropertyEntry lookAheadObj = orderByProperty.OrderMatrix[lookAhead];
|
|
|
|
if (orderByProperty.Comparer.Compare(currentObj, lookAheadObj) == 0)
|
|
{
|
|
orderByProperty.OrderMatrix.RemoveAt(lookAhead);
|
|
}
|
|
else
|
|
{
|
|
current = lookAhead;
|
|
currentObj = lookAheadObj;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
protected override void EndProcessing()
|
|
{
|
|
OrderByProperty orderByProperty = new OrderByProperty(
|
|
this, InputObjects, Property, !Descending, ConvertedCulture, CaseSensitive);
|
|
if (orderByProperty.Comparer == null || orderByProperty.OrderMatrix == null || orderByProperty.OrderMatrix.Count == 0)
|
|
{
|
|
return;
|
|
}
|
|
orderByProperty.OrderMatrix.Sort(orderByProperty.Comparer);
|
|
|
|
if (unique)
|
|
{
|
|
RemoveDuplicates(orderByProperty);
|
|
}
|
|
|
|
// write to output stream
|
|
foreach (OrderByPropertyEntry x in orderByProperty.OrderMatrix)
|
|
{
|
|
WriteObject(x.inputObject);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|