对枚举阵列进行排序

  • 本文关键字:排序 阵列 枚举 c#
  • 更新时间 :
  • 英文 :


你好,我有一系列枚举,我正在尝试按数组的枚举价值排序该数组,并获取顶部的数组索引

private enum Values
{
LOW,
MEDIUM,
HIGH
}
private Values[] Settings = new Values[10];
Settings[0] = LOW;
Settings[1] = HIGH;
Settings[2] = MEDIUM;
Settings[3] = LOW;
Settings[4] = LOW;
Settings[5] = LOW;
Settings[6] = LOW;
Settings[7] = LOW;
Settings[8] = MEDIUM;
Settings[9] = MEDIUM;

基本上,现在,有了我上面的内容,我需要按枚举值对设置数组进行排序,并获取顶部的数组索引(例如3(项目;

所以我会得到值1、2、8

我正在使用的平台不支持LINQ,因此这些方便的功能不可用。

我一直在试图缠绕我的头,但是再有一双眼睛会有所帮助。

谢谢。

实现包装参考类型,

class ValueWrapper : IComparable<ValueWrapper>
{
    public Values Value { get; set; }
    public int Index { get; set; }
    public int CompareTo(ValueWrapper other)
    {
        return this.Value.CompareTo(other.Value) * -1; // Negating since you want reversed order
    }
}

用法 -

ValueWrapper[] WrappedSettings = new ValueWrapper[10];
for(int i = 0; i < WrappedSettings.Length; i++)
{
    WrappedSettings[i] = new ValueWrapper { Value = Settings[i], Index = i };
}
Array.Sort(WrappedSettings);

WrappedSettings将按照您指定进行排序,并保留它们在原始数组中的索引。

怎么样:

Values first = Values.Low,second = Values.Low,third = Values.Low;
int firstI = -1,secondI = -1, thirdI = -1;
for(int i = 0;i < Settings.Length;i++)
{
    if(Settings[i] > first || firstI == -1)
    {
        third = second;
        thirdI = secondI;
        second= first;
        secondI= firstI;
        first = Settings[i];
        firstI = i;
    } 
    else if(Settings[i] > second || secondI == -1)
    {
        third = second;
        thirdI = secondI;
        second = Settings[i];
        secondI = i;
    } 
    else if(Settings[i] > third || thirdI == -1)
    {
        third = Settings[i];
        thirdI = i;
    }
}

因此,由于您说您在不可用的LINQ的环境中工作,所以我认为其他事物诸如仿制药,无销售等方面也不可用。一个非常低科技的解决方案。

基本思想:

对于从最高到最低的每个可能的枚举值,都可以浏览列表。如果我们找到该值,请输出并记住我们有多少输出。如果我们达到3,请停止算法。

所以,我们首先在列表中寻找高级,然后在中等范围内等。

class Program
{
    private enum Values
    {
        LOW,
        MEDIUM,
        HIGH
    }

    static void Main(string[] args)
    {
        // Sample data
        Values[] settings = new Values[10];
        settings[0] = Values.LOW;
        settings[1] = Values.HIGH;
        settings[2] = Values.MEDIUM;
        settings[3] = Values.LOW;
        settings[4] = Values.LOW;
        settings[5] = Values.LOW;
        settings[6] = Values.LOW;
        settings[7] = Values.LOW;
        settings[8] = Values.MEDIUM;
        settings[9] = Values.MEDIUM;

        // Get Values of the enum type
        // This list is sorted ascending by value but may contain duplicates
        Array enumValues = Enum.GetValues(typeof(Values));
        // Number of results found so far
        int numberFound = 0;
        // The enum value we used during the last outer loop, so
        // we skip duplicate enum values
        int lastValue = -1;
        // For each enum value starting with the highest to the lowest
        for (int i= enumValues.Length -1; i >= 0; i--)
        {
            // Get this enum value
            int enumValue = (int)enumValues.GetValue(i);
            // Check whether we had the same value in the previous loop
            // If yes, skip it.
            if(enumValue == lastValue)
            {
                continue;
            }
            lastValue = enumValue;
            // For each entry in the list where we are searching
            for(int j=0; j< settings.Length; j++)
            {
                // Check to see whether it is the currently searched value
                if (enumValue == (int)settings[j])
                {
                    // if yes, then output it.
                    Console.WriteLine(j);
                    numberFound++;
                    // Stop after 3 found entries
                    if (numberFound == 3)
                    {
                        goto finished;
                    }
                }
            }
        }
        finished: 
        Console.ReadLine();
    }
}

输出按要求1,2,8

我不确定这是否正是您想要的另一个数组中最高值的索引。然后,我们可以循环遍历原始数组,对于每个项目,查看它是否比到目前为止存储的索引上的任何项目都要大。如果是,请与该项目交换。

例如:

// Start the topIndexes array with all invalid indexes
var topIndexes = new[] {-1, -1, -1};
for (var settingIndex = 0; settingIndex < Settings.Length; settingIndex++)
{
    var setting = Settings[settingIndex];
    var topIndexLessThanSetting = -1;
    // Find the smallest topIndex setting that's less than this setting
    for (int topIndex = 0; topIndex < topIndexes.Length; topIndex++)
    {
        if (topIndexes[topIndex] == -1)
        {
            topIndexLessThanSetting = topIndex;
            break;
        }
        if (setting <= Settings[topIndexes[topIndex]]) continue;
        if (topIndexLessThanSetting == -1 ||
            Settings[topIndexes[topIndex]] < Settings[topIndexes[topIndexLessThanSetting]])
        {
            topIndexLessThanSetting = topIndex;
        }
    }
    topIndexes[topIndexLessThanSetting] = settingIndex;
}
// topIndexes = { 1, 2, 8 }

最新更新