比较 GUID,以便我可以按 GUID 排序

发布于 2024-08-14 05:01:54 字数 319 浏览 1 评论 0原文

有什么好的、快速的方法来对 GUID 列表(如 TGuid)进行排序。我想我只是使用 SysUtils.CompareMem(P1, P2: Pointer; Length: Integer): Boolean;直到我意识到它返回布尔值。

我希望有一个类似于 CompareText( ) 或 CompareValue( ) 的东西,它返回整数,以便它可以用于排序比较。

我想没有多少人希望对 GUID 进行排序...有什么想法吗?

我想我可以对 TGuid 记录的内容进行一些级联调用 CompareValue()。我的直觉告诉我一定有更好的方法!

谢谢你!

what's a nice, fast way to sort a list of GUIDs (as TGuid). i thought i'd just use SysUtils.CompareMem(P1, P2: Pointer; Length: Integer): Boolean; until i realized it returns boolean.

i'd wish for something comparable to CompareText( ) or CompareValue( ) that return integer so it can be used in a sort comparison.

i suppose not many people wish to sort GUIDs...any ideas?

i suppose i could call make some cascading calls to CompareValue( ) on the contents of the TGuid record. my instincts tell me there must be a better way!

thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

凉风有信 2024-08-21 05:01:54

如果您使用的是 Delphi 2009 或更高版本,则可以使用 Generics.Defaults 单元中的 TComparer.Compare() 或其调用的 BinaryCompare 函数。

If you're using Delphi 2009 or better, you can use TComparer<TGUID>.Compare(), or the BinaryCompare function it calls, from the Generics.Defaults unit.

似狗非友 2024-08-21 05:01:54

我不知道 Delphi,但通常 GUID 是 128 位十六进制字符串,您可以将子元素转换/解析为无符号(4 * 4 字节或2 * 8 字节)整数,然后比较它们。一旦您拥有该函数,只需应用标准排序算法即可。

如果我的答案不满足此处介绍的 Microsoft 使用的 GUID 规范 的 RFC,您也许可以想出更好的方法来排序提取 GUID 中的位级数据。

I do not know Delphi but generally a GUID is a 128-bit hexadecimal string, you can just cast/parse the sub-elements to unsigned (4 * 4-byte or 2*8-byte) integers and then compare them. Once you have that function just apply a standard sort algorithm.

If my answer does not satisfy the RFC of the GUID specification Microsoft uses is presented here, you can probably come up with better ways of sorting extracting the bit-level data in the GUID.

叹梦 2024-08-21 05:01:54

使用 GUIDToString 并对其进行 CompareStr ——不是最快的选项,但它可以工作。

Use GUIDToString and do CompareStr on that -- not the fastest option but it works.

_蜘蛛 2024-08-21 05:01:54
Function CompareGUIDS( pvGUID1, pvGUID2 : TGUID ) : Boolean;
 Begin
   If ( pvGUID1.D1    = pvGUID2.D1 )    And
      ( pvGUID1.D2    = pvGUID2.D2 )    And
      ( pvGUID1.D3    = pvGUID2.D3 )    And
      ( pvGUID1.D4[ 0 ] = pvGUID2.D4[ 0 ] ) And
      ( pvGUID1.D4[ 1 ] = pvGUID2.D4[ 1 ] ) And
      ( pvGUID1.D4[ 2 ] = pvGUID2.D4[ 2 ] ) And
      ( pvGUID1.D4[ 3 ] = pvGUID2.D4[ 3 ] ) And
      ( pvGUID1.D4[ 4 ] = pvGUID2.D4[ 4 ] ) And
      ( pvGUID1.D4[ 5 ] = pvGUID2.D4[ 5 ] ) And
      ( pvGUID1.D4[ 6 ] = pvGUID2.D4[ 6 ] ) And
      ( pvGUID1.D4[ 7 ] = pvGUID2.D4[ 7 ] ) Then
      Result := True
   Else
      Result := False;
 End;
Function CompareGUIDS( pvGUID1, pvGUID2 : TGUID ) : Boolean;
 Begin
   If ( pvGUID1.D1    = pvGUID2.D1 )    And
      ( pvGUID1.D2    = pvGUID2.D2 )    And
      ( pvGUID1.D3    = pvGUID2.D3 )    And
      ( pvGUID1.D4[ 0 ] = pvGUID2.D4[ 0 ] ) And
      ( pvGUID1.D4[ 1 ] = pvGUID2.D4[ 1 ] ) And
      ( pvGUID1.D4[ 2 ] = pvGUID2.D4[ 2 ] ) And
      ( pvGUID1.D4[ 3 ] = pvGUID2.D4[ 3 ] ) And
      ( pvGUID1.D4[ 4 ] = pvGUID2.D4[ 4 ] ) And
      ( pvGUID1.D4[ 5 ] = pvGUID2.D4[ 5 ] ) And
      ( pvGUID1.D4[ 6 ] = pvGUID2.D4[ 6 ] ) And
      ( pvGUID1.D4[ 7 ] = pvGUID2.D4[ 7 ] ) Then
      Result := True
   Else
      Result := False;
 End;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文