Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 基于键自动排序对象的数据结构?_C# - Fatal编程技术网

C# 基于键自动排序对象的数据结构?

C# 基于键自动排序对象的数据结构?,c#,C#,我希望有一个结构可以根据关联的键为我自动排序数据,但一旦这样做了,我就不必用键抓取任何对象,我只想从列表中删除第一个对象。在我的特定情况下,每个对象都有一个关联的浮点值,我想将它们从低到高排序 例如,我希望能够对整数列表进行排序,但要按其相应的浮点“键”,并获取索引0处的一个(这将是具有最低关联浮点数的一个) 我遇到过orderedDictionary,但我不完全理解它们,也不知道它们是否适合我的需要。我以为它们只是一个字典,允许你也索引到它们,但它们不是一个模板类 您可能需要一个分类数据集:

我希望有一个结构可以根据关联的键为我自动排序数据,但一旦这样做了,我就不必用键抓取任何对象,我只想从列表中删除第一个对象。在我的特定情况下,每个对象都有一个关联的浮点值,我想将它们从低到高排序

例如,我希望能够对整数列表进行排序,但要按其相应的浮点“键”,并获取索引0处的一个(这将是具有最低关联浮点数的一个)


我遇到过orderedDictionary,但我不完全理解它们,也不知道它们是否适合我的需要。我以为它们只是一个字典,允许你也索引到它们,但它们不是一个模板类

您可能需要一个分类数据集:

如果您未使用.net 4.0,则可在PowerCollection项目中使用它:

使用.Net4.0 SortedSet的示例

SortedSet<float> set = new SortedSet<float>( );
set.Add(13.3f);
set.Add(0.5f);
set.Add(5.5f);

Console.WriteLine(string.Format("Minimum Value: {0}", set.Min)); // prints 0.5
Console.WriteLine(string.Format("Maximum Value: {0}", set.Max)); // prints 13.3

foreach (float f in set)
{
    Console.WriteLine(f);
}
// prints:
// 0.5
// 5.5
// 13.3

// using custom IComparer<float>, see implementation below
set = new SortedSet<float>(new FloatDescComparere());

set.Add(13.3f);
set.Add(0.5f);
set.Add(5.5f);

Console.WriteLine(string.Format("Minimum Value: {0}", set.Min)); // prints 13.3
Console.WriteLine(string.Format("Maximum Value: {0}", set.Max)); // prints 0.5

foreach (float f in set)
{
    Console.WriteLine(f);
}
// prints:
// 13.3
// 5.5
// 0.5
SortedSet set=新的SortedSet();
增加(13.3f);
设置。添加(0.5f);
增加(5.5f);
Console.WriteLine(string.Format(“最小值:{0}”,set.Min));//打印0.5
Console.WriteLine(string.Format(“最大值:{0}”,set.Max));//印刷品13.3
foreach(集合中的浮动f)
{
控制台写入线(f);
}
//印刷品:
// 0.5
// 5.5
// 13.3
//使用自定义IComparer,请参见下面的实现
设置=新的分类集(新的floatDescomare());
增加(13.3f);
设置。添加(0.5f);
增加(5.5f);
Console.WriteLine(string.Format(“最小值:{0}”,set.Min));//印刷品13.3
Console.WriteLine(string.Format(“最大值:{0}”,set.Max));//打印0.5
foreach(集合中的浮动f)
{
控制台写入线(f);
}
//印刷品:
// 13.3
// 5.5
// 0.5
描述/比较:

private class FloatDescComparere : IComparer<float>
{
    public int Compare(float x, float y)
    {
        if (y > x)
            return 1;
        else if (x > y)
            return -1;
        else
            return 0;
    }
}
private类floatdesccomparer:IComparer
{
公共整数比较(浮点x,浮点y)
{
如果(y>x)
返回1;
如果(x>y),则为else
返回-1;
其他的
返回0;
}
}

您可能需要一个分类数据集:

如果您未使用.net 4.0,则可在PowerCollection项目中使用它:

使用.Net4.0 SortedSet的示例

SortedSet<float> set = new SortedSet<float>( );
set.Add(13.3f);
set.Add(0.5f);
set.Add(5.5f);

Console.WriteLine(string.Format("Minimum Value: {0}", set.Min)); // prints 0.5
Console.WriteLine(string.Format("Maximum Value: {0}", set.Max)); // prints 13.3

foreach (float f in set)
{
    Console.WriteLine(f);
}
// prints:
// 0.5
// 5.5
// 13.3

// using custom IComparer<float>, see implementation below
set = new SortedSet<float>(new FloatDescComparere());

set.Add(13.3f);
set.Add(0.5f);
set.Add(5.5f);

Console.WriteLine(string.Format("Minimum Value: {0}", set.Min)); // prints 13.3
Console.WriteLine(string.Format("Maximum Value: {0}", set.Max)); // prints 0.5

foreach (float f in set)
{
    Console.WriteLine(f);
}
// prints:
// 13.3
// 5.5
// 0.5
SortedSet set=新的SortedSet();
增加(13.3f);
设置。添加(0.5f);
增加(5.5f);
Console.WriteLine(string.Format(“最小值:{0}”,set.Min));//打印0.5
Console.WriteLine(string.Format(“最大值:{0}”,set.Max));//印刷品13.3
foreach(集合中的浮动f)
{
控制台写入线(f);
}
//印刷品:
// 0.5
// 5.5
// 13.3
//使用自定义IComparer,请参见下面的实现
设置=新的分类集(新的floatDescomare());
增加(13.3f);
设置。添加(0.5f);
增加(5.5f);
Console.WriteLine(string.Format(“最小值:{0}”,set.Min));//印刷品13.3
Console.WriteLine(string.Format(“最大值:{0}”,set.Max));//打印0.5
foreach(集合中的浮动f)
{
控制台写入线(f);
}
//印刷品:
// 13.3
// 5.5
// 0.5
描述/比较:

private class FloatDescComparere : IComparer<float>
{
    public int Compare(float x, float y)
    {
        if (y > x)
            return 1;
        else if (x > y)
            return -1;
        else
            return 0;
    }
}
private类floatdesccomparer:IComparer
{
公共整数比较(浮点x,浮点y)
{
如果(y>x)
返回1;
如果(x>y),则为else
返回-1;
其他的
返回0;
}
}

您可以使用哈希表,将“key”放在哈希表中,并在哈希表中搜索元素“key”,如果哈希表中有key,那么您就有了该元素。每次添加新元素O(1)时都必须更新,但查找复杂度也为O(1)。

您可以使用哈希表,将“key”放入哈希中,并在哈希中搜索元素“key”,如果哈希中包含该元素的key。每次添加新元素O(1)时都必须进行更新,但查找复杂性也为O(1)。

一旦列表填充一次,是否会向列表中添加新项目?关于
SortedList
OrderedDictionary
,您不了解什么?您还没有完全解释您的用例。因此,如果您有一堆对,您希望按浮点排序,但也可以按int快速访问,对吗?是的,我将添加新项,但我不需要通过int访问-只需获取具有最低关联浮点值的项。一旦列表填充一次,您是否会将新项添加到列表中?关于
SortedList
OrderedDictionary
,您不了解什么?您还没有完全解释您的用例。因此,如果您有一堆对,您希望按浮点排序,但也可以按int快速访问,对吗?是的,我将添加新项目,但我不需要按int访问-只需获取关联浮点值最低的一个。我完全忘记了他想要排序,我只是为了寻找答案。然后对元素进行排序并应用散列。我完全忘记了他想要排序,我只回答了查找部分。然后对元素进行排序并应用散列。你能解释一下它们是什么吗?我添加了一个关于如何使用.NET4.0中的SortedSet的示例。你能解释一下它们是什么吗?我添加了一个关于如何使用.NET4.0中的SortedSet的示例