C# 使用SortedGuide字符串失败!"); 返回; } } WriteLine(“使用sortedGuidStrings排序成功!”); } 运行您的精确示例,我能够在x86上生成顺序GUID。无法复制。请告诉更多关于系统设置的信息您需要为每个迭代进行长
使用SortedGuide字符串失败!"); 返回; } } WriteLine(“使用sortedGuidStrings排序成功!”); }C# 使用SortedGuide字符串失败!"); 返回; } } WriteLine(“使用sortedGuidStrings排序成功!”); } 运行您的精确示例,我能够在x86上生成顺序GUID。无法复制。请告诉更多关于系统设置的信息您需要为每个迭代进行长,c#,guid,C#,Guid,使用SortedGuide字符串失败!"); 返回; } } WriteLine(“使用sortedGuidStrings排序成功!”); } 运行您的精确示例,我能够在x86上生成顺序GUID。无法复制。请告诉更多关于系统设置的信息您需要为每个迭代进行长时间睡眠,有了睡眠,您的睡眠被分类了。删除了睡眠。一切仍然很好。结果抱歉,我的意思是睡眠不起作用有任何特殊原因吗请等一分钟生成guid?我真的不愿意现在再等10分钟来重现它。但我认为,它不起作用,因为序列可能会在如此长的时间中断。因此我无法使用
运行您的精确示例,我能够在x86上生成顺序GUID。无法复制。请告诉更多关于系统设置的信息您需要为每个迭代进行长时间睡眠,有了睡眠,您的睡眠被分类了。删除了睡眠。一切仍然很好。结果抱歉,我的意思是睡眠不起作用有任何特殊原因吗请等一分钟生成guid?我真的不愿意现在再等10分钟来重现它。但我认为,它不起作用,因为序列可能会在如此长的时间中断。因此我无法使用默认的比较器对guid进行排序?UUIDCreateSequential返回序列guid,我想我可以使用默认的比较器对它们进行排序guid compareras据我所知,它使用MAC地址来保证唯一性和时间来保证顺序性,“你有一个服务,它提供的数字在宇宙中所有计算机上都是连续和唯一的。。。这不是一个神奇的盒子;必须有人编写代码。”-我以为有一个生成所有数字的程序!问题是关于排序的guid而不是顺序模式。因此,生成的guid不是使用默认guid排序的
private class NativeMethods
{
[DllImport("rpcrt4.dll", SetLastError = true)]
public static extern int UuidCreateSequential(out Guid guid);
}
public static Guid CreateSequentialGuid()
{
const int RPC_S_OK = 0;
Guid guid;
int result = NativeMethods.UuidCreateSequential(out guid);
if (result == RPC_S_OK)
return guid;
else throw new Exception("could not generate unique sequential guid");
}
static void TestSortedSequentialGuid(int length)
{
Guid []guids = new Guid[length];
int[] ids = new int[length];
for (int i = 0; i < length; i++)
{
guids[i] = CreateSequentialGuid();
ids[i] = i;
Thread.Sleep(60000);
}
Array.Sort(guids, ids);
for (int i = 0; i < length - 1; i++)
{
if (ids[i] > ids[i + 1])
{
Console.WriteLine("sorting using guids failed!");
return;
}
}
Console.WriteLine("sorting using guids succeeded!");
}
"53cd98f2504a11e682838cdcd43024a7",
"7178df9d504a11e682838cdcd43024a7",
"800b5b69504a11e682838cdcd43024a7",
"9796eb73504a11e682838cdcd43024a7",
"c14c5778504a11e682838cdcd43024a7",
"c14c5779504a11e682838cdcd43024a7",
"d2324e9f504a11e682838cdcd43024a7",
"d2324ea0504a11e682838cdcd43024a7",
"da3d4460504a11e682838cdcd43024a7",
"e149ff28504a11e682838cdcd43024a7",
"f2309d56504a11e682838cdcd43024a7",
"f2309d57504a11e682838cdcd43024a7",
"fa901efd504a11e682838cdcd43024a7",
"fa901efe504a11e682838cdcd43024a7",
"036340af504b11e682838cdcd43024a7",
"11768c0b504b11e682838cdcd43024a7",
"2f57689d504b11e682838cdcd43024a7"
using System;
using System.Linq;
using System.Numerics;
class Program
{
static void Main(string[] args)
{
//These are the sequential guids you provided.
Guid[] guids = new[]
{
"53cd98f2504a11e682838cdcd43024a7",
"7178df9d504a11e682838cdcd43024a7",
"800b5b69504a11e682838cdcd43024a7",
"9796eb73504a11e682838cdcd43024a7",
"c14c5778504a11e682838cdcd43024a7",
"c14c5779504a11e682838cdcd43024a7",
"d2324e9f504a11e682838cdcd43024a7",
"d2324ea0504a11e682838cdcd43024a7",
"da3d4460504a11e682838cdcd43024a7",
"e149ff28504a11e682838cdcd43024a7",
"f2309d56504a11e682838cdcd43024a7",
"f2309d57504a11e682838cdcd43024a7",
"fa901efd504a11e682838cdcd43024a7",
"fa901efe504a11e682838cdcd43024a7",
"036340af504b11e682838cdcd43024a7",
"11768c0b504b11e682838cdcd43024a7",
"2f57689d504b11e682838cdcd43024a7"
}.Select(l => Guid.Parse(l)).ToArray();
//Convert to BigIntegers to get their numeric value from the Guids bytes then sort them.
BigInteger[] values = guids.Select(l => new BigInteger(l.ToByteArray())).OrderBy(l => l).ToArray();
for (int i = 0; i < guids.Length; i++)
{
//Convert back to a guid.
Guid sortedGuid = new Guid(values[i].ToByteArray());
//Compare the guids. The guids array should be sequential.
if(!sortedGuid.Equals(guids[i]))
throw new Exception("Not sequential!");
}
Console.WriteLine("All good!");
Console.ReadKey();
}
}
static void TestSortedSequentialGuid(int length)
{
Guid []guids = new Guid[length];
int[] ids = new int[length];
for (int i = 0; i < length; i++)
{
guids[i] = CreateSequentialGuid();
ids[i] = i;
// this simulates the delay between guids creation
// yes the guids will not be sequential as it interrupts generator
// (as it used the time internally)
// but still the guids should be in increasing order and hence they are
// sortable and that was the goal of the question
Thread.Sleep(60000);
}
var sortedGuidStrings = guids.Select(x =>
{
var bytes = x.ToByteArray();
//reverse high bytes that represents the sequential part (time)
string high = BitConverter.ToString(bytes.Take(10).Reverse().ToArray());
//set last 6 bytes are just the node (MAC address) take it as it is.
return high + BitConverter.ToString(bytes.Skip(10).ToArray());
}).ToArray();
// sort ids using the generated sortedGuidStrings
Array.Sort(sortedGuidStrings, ids);
for (int i = 0; i < length - 1; i++)
{
if (ids[i] > ids[i + 1])
{
Console.WriteLine("sorting using sortedGuidStrings failed!");
return;
}
}
Console.WriteLine("sorting using sortedGuidStrings succeeded!");
}