C# 列出问题的词典
我用通过第三方对象解析的数据填写以下词典 在我的解析类中,它看起来像这样:C# 列出问题的词典,c#,xcode,linq,dictionary,C#,Xcode,Linq,Dictionary,我用通过第三方对象解析的数据填写以下词典 在我的解析类中,它看起来像这样: public string beaconMajor; public string[] beaconValues; public Dictionary<string, float> valueDictionary = new Dictionary<string, float>(); public string foo = ""; public void DistanceValue(string m
public string beaconMajor;
public string[] beaconValues;
public Dictionary<string, float> valueDictionary = new Dictionary<string, float>();
public string foo = "";
public void DistanceValue(string message)
{
foo = message.Split (',');
foreach (string b in foo)
{
beaconValues = beacon.Split('_');
beaconMajor = beaconValues[0];
string beaconDistance = beaconValues[1];
if(valueDictionary.ContainsKey(beaconMajor))
{
valueDictionary[beaconMajor] = float.Parse(beaconDistance);
}
else
{
valueDictionary.Add(beaconMajor, float.Parse(beaconDistance));
}
}
if (foo.Contains("-1"))
{
return;
}
}
现在我想做的是,按照字典的值而不是键来排序。键是常量,但值每隔几秒钟更改一次。我尝试了以下代码:
List<KeyValuePair<string, float>> list = foo_class.valueDictionary.ToList();
list.Sort
(delegate(KeyValuePair<string, float> val1, KeyValuePair<string, float> val2)
{
return val1.Value.CompareTo(val2.Value);
}
);
Debug.Log(list);
dictionaryString = list.ToString() + "\n";
List List=foo_class.valueDictionary.ToList();
列表。排序
(代理(KeyValuePair val1、KeyValuePair val2)
{
返回val1.Value.CompareTo(val2.Value);
}
);
调试日志(列表);
dictionaryString=list.ToString()+“\n”;
但当我运行此命令时,我在xCode中得到以下消息:
System.Collections.Generic.List1[System.Collections.Generic.KeyValuePair
2[System.String,System.Single]]
调试:内部日志(Int32,字符串,对象)
调试:日志(对象)三角剖分脚本:更新()
有人能告诉我这个消息的原因吗
我仍在研究如何让这个字典通过值对自己进行排序,我甚至尝试过使用LINQ,但这与xCode不兼容,我在xCode中看到一条JIT错误消息,如下所示:
ExecutionEngineeException:正在尝试JIT编译方法
'System.Linq.OrderedEnumerable1:GetEnumerator
()运行时使用--aot only
如果有人知道其他方法,请告诉我。我已经在这上面花了好几天了
更新
我使用了下面的建议答案,并在xcode中得到了这条消息:
ExecutionEngineeException:正在尝试JIT编译方法
“System.Linq.OrderedEnumerable1:GetEnumerator()”同时仅使用--aot运行
在
System.Collections.Generic.List1[System.Collections.Generic.KeyValuePair
2[System.String,System.Single]].AddEnumerable
(IEnumerable1可枚举)[0x00000]英寸:0英寸
System.Collections.Generic.List
1[System.Collections.Generic.KeyValuePair2[System.String,System.Single]
(IEnumerable
1集合)[0x00000]位于:0处
System.Linq.Enumerable.ToList[KeyValuePair2](IEnumerable
1源代码)
[0x00000]in:0位于TriangulationScript.Update()
[0x00000]英寸:0
试着这样做:
if (foo_class.valueDictionary.ContainsKey ("key"))
{
beacon1Scale = float.Parse (foo_class.valueDictionary["key"].ToString());
}
List<KeyValuePair<string, float>> list = items
.OrderBy(x => x.Value)
.ToList<KeyValuePair<string, float>>();
List=项目
.OrderBy(x=>x.Value)
.ToList();
list.ToString()
不会像您认为的那样执行操作-看起来您希望list.ToString()
将整个列表的内容输出到字符串中。相反,它使用了object.ToString()
的默认行为,即发出对象的完全限定类型名,在本例中为
System.Collections.Generic.List`1[System.Collections.Generic.KeyValuePair`2[System.String,System.Single]]
我仍在研究如何让这本字典通过这些值对自己进行排序
Dictionary
没有顺序-如果您想以特定顺序输出kay/值对,您必须在每次迭代列表时指定该顺序,或者存储为键/值对列表,而该列表没有可用的Dictionary
特定方法(如索引查找)OrderBy是Linq,我以前也用过。这就是导致JIT消息匹配的原因。然后,尝试使用我的github repo的某种排序算法。但是你需要把它们和字典一起使用;)回滚编辑,因为上面的代码需要在xCode中编译,这就是为什么我不能使用LINQ。如果有人知道这样做的方法,请让我知道。OT:如何使用c#in xcode?与Unity一起使用xcode插件。我所有的C#都是在团结一方完成的。