SQL Server GUID sort algorithm. Why?

前端 未结 3 1051
一生所求
一生所求 2020-11-28 08:52

Problem with UniqueIdentifiers

We have an existing database which uses uniqueidentifiers extensively (unfortunately!) both as primary keys and some

3条回答
  •  天涯浪人
    2020-11-28 09:05

    Here's a different approach. The GUID is simply shuffled around ready for a normal string comparison like it occurs in SQL Server. This is Javascript but it is very easy to convert to any language.

    function guidForComparison(guid) {
      /*
      character positions:  
                11111111112222222222333333
      012345678901234567890123456789012345
    
      00000000-0000-0000-0000-000000000000
    
      byte positions:  
                              111111111111
      00112233 4455 6677 8899 001122334455
      */
      return guid.substr(24, 12) + 
             guid.substr(19, 4) + 
             guid.substr(16, 2) + 
             guid.substr(14, 2) + 
             guid.substr(11, 2) + 
             guid.substr(9, 2) + 
             guid.substr(6, 2) +
             guid.substr(4, 2) +
             guid.substr(2, 2) +
             guid.substr(0, 2);
    };
    

提交回复
热议问题