[all scores showhand]一个关于排序的算法问题
1 UserInfo类public class UserInfo
{
public string UserName;
public DateTime TimeStamp;
}
2 Dictionary<String, UserInfo> ucObject = new Dictionary<String, UserInfo>;
ucObject.Add("u1",UserInfo1);
ucObject.Add("u2",UserInfo2);
……
3 问,如何找到ucObject里UserInfo的TimeStamp最小的那个。
本来用bubble轻易搞定,但是Dictionary<>无索引器,如果要做Bubble,还要再次将ucObject里的对象价格索引再Bubble。
看看有没朋友能写个开销最小,速度最快的算法,以得到ucObject里UserInfo的TimeStamp最小的那个对象。
很多年没来过了,手头上的分showhand了。 --------------------编程问答-------------------- SortedDictionary --------------------编程问答-------------------- SortedDictionary.Min() --------------------编程问答-------------------- 参考MSDN:
下面的代码示例演示如何使用 Dictionary<(Of <(TKey, TValue>)>)(IEqualityComparer<(Of <(TKey>)>)) 构造函数,利用来自另一个字典的已排序内容初始化 Dictionary<(Of <(TKey, TValue>)>)。该代码示例创建一个 SortedDictionary<(Of <(TKey, TValue>)>) 并在其中随机填充数据,然后将 SortedDictionary<(Of <(TKey, TValue>)>) 传递给 Dictionary<(Of <(TKey, TValue>)>)(IEqualityComparer<(Of <(TKey>)>)) 构造函数,创建一个经过排序的 Dictionary<(Of <(TKey, TValue>)>)。如果需要生成一个某些时候是静态的排序字典,这将十分有用;将数据从 SortedDictionary<(Of <(TKey, TValue>)>) 复制到 Dictionary<(Of <(TKey, TValue>)>) 可提高检索速度。
using System;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
// Create a new sorted dictionary of strings, with string
// keys.
SortedDictionary<string, string> openWith =
new SortedDictionary<string, string>();
// Add some elements to the dictionary.
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");
// Create a Dictionary of strings with string keys, and
// initialize it with the contents of the sorted dictionary.
Dictionary<string, string> copy =
new Dictionary<string, string>(openWith);
// List the contents of the copy.
Console.WriteLine();
foreach( KeyValuePair<string, string> kvp in copy )
{
Console.WriteLine("Key = {0}, Value = {1}",
kvp.Key, kvp.Value);
}
}
}
/* This code example produces the following output:
Key = bmp, Value = paint.exe
Key = dib, Value = paint.exe
Key = rtf, Value = wordpad.exe
Key = txt, Value = notepad.exe
*/
--------------------编程问答-------------------- 楼上仁兄?SortedDisctionary有Min()方法?
已经搞定:
SortedList泛型可以使用index来获取。
Another difference between the SortedDictionary and SortedList classes is that SortedList supports efficient indexed retrieval of keys and values through the collections returned by the Keys and Values properties. It is not necessary to regenerate the lists when the properties are accessed, because the lists are just wrappers for the internal arrays of keys and values. The following code shows the use of the Values property for indexed retrieval of values from a sorted list of strings:
--------------------编程问答-------------------- sorry,夹层了。
发现csdn这个论坛有点垃圾(用了一些自以为是的技术但是不适用)。。-_-!!!
wxg:你的答复并没解决lz问题。
--------------------编程问答-------------------- 抱歉我的回复帮不到LZ
但起码查了下,有点参考价值,
对看贴的朋友也会有点帮助的不是吗?
PS:与CSDN无关吧 --------------------编程问答--------------------
http://msdn2.microsoft.com/zh-cn/library/bb909852.aspx
--------------------编程问答-------------------- //未经过测试,自忖:
应该不能吧,由于LZ要比较的是UserInfo类中的的某个属性,不遍历能比较出来??!! --------------------编程问答-------------------- 可以排序的集合处理办法到处都是啊!!
最简单的Array也有啊!!
Array.Short();// --------------------编程问答-------------------- C# 学生..正在学习中..
接分..来点分吧..哈哈.. --------------------编程问答-------------------- 排序字典解决不了问题么?
解决了?
接分 :)
补充:.NET技术 , C#