logo
Tags down

shadow

How to sort GUID's the SQL Server way using Delphi


By : Miguelzvd
Date : October 17 2020, 11:12 AM
With these it helps Alberto Ferrari has written a blog entry about it. From there:
code :


Share : facebook icon twitter icon

Are GUIDs necessary to use interfaces in Delphi?


By : user2047063
Date : March 29 2020, 07:55 AM
I wish this helpful for you I've noticed that some methods such as Supports (to determine if a class conforms to a specific interface) require that you define a GUID before you can use them.
This page confirms it with the following information:

With mongodb and guids for the Id of documents what is efficient way to store the Guids to easily retrieve the actual Gu


By : Farhan Ahmad
Date : March 29 2020, 07:55 AM
I wish did fix the issue. Working with GUIDs has a few pitfalls, mostly related to how to work with the binary representation in the mongo shell and also to historical accidents which resulted in different drivers storing GUIDs using different byte orders.
I used the following code to illustrate the issues:
code :
var document = new BsonDocument { { "_id", Guid.NewGuid() }, { "x", 1 } };
collection.Drop();
collection.Insert(document);
Console.WriteLine("Inserted GUID: {0}", document["_id"].AsGuid);
Inserted GUID: 2d25b9c6-6d30-4441-a360-47e7804c62be
> var doc = db.test.findOne()
> doc
{ "_id" : BinData(3,"xrklLTBtQUSjYEfngExivg=="), "x" : 1 }
> doc._id.hex()
c6b9252d306d4144a36047e7804c62be
>
C:\mongodb\mongodb-win32-x86_64-2.0.6\bin>mongo --shell uuidhelpers.js
MongoDB shell version: 2.0.6
connecting to: test
type "help" for help
> var doc = db.test.findOne()
> doc
{ "_id" : BinData(3,"xrklLTBtQUSjYEfngExivg=="), "x" : 1 }
> doc._id.hex()
c6b9252d306d4144a36047e7804c62be
> doc._id.toCSUUID()
CSUUID("2d25b9c6-6d30-4441-a360-47e7804c62be")
>
> db.test.find({_id : CSUUID("2d25b9c6-6d30-4441-a360-47e7804c62be")})
{ "_id" : BinData(3,"xrklLTBtQUSjYEfngExivg=="), "x" : 1 }
>

How to sort sequential GUIDs in C#?


By : Tael
Date : March 29 2020, 07:55 AM
To fix the issue you can do There is a difference between the way Sql server and .NET sort guids.
There is a struct in the .NET framework called SqlGuid that should behave the same way as guids in Sql Server.
code :
List<Guid> a = new List<Guid>();
a.Add(new Guid("3AAAAAAA-BBBB-CCCC-DDDD-2EEEEEEEEEEE"));
a.Add(new Guid("2AAAAAAA-BBBB-CCCC-DDDD-1EEEEEEEEEEE"));
a.Add(new Guid("1AAAAAAA-BBBB-CCCC-DDDD-3EEEEEEEEEEE"));
Console.WriteLine("--Unsorted Guids--");
foreach (Guid g in a)
{
    Console.WriteLine("{0}", g);
}
a.Sort();
Console.WriteLine("--Sorted Guids--");
foreach (Guid g in a)
{
    Console.WriteLine("{0}", g);
}

List<SqlGuid> b = new List<SqlGuid>();
b.Add(new SqlGuid("3AAAAAAA-BBBB-CCCC-DDDD-2EEEEEEEEEEE"));
b.Add(new SqlGuid("2AAAAAAA-BBBB-CCCC-DDDD-1EEEEEEEEEEE"));
b.Add(new SqlGuid("1AAAAAAA-BBBB-CCCC-DDDD-3EEEEEEEEEEE"));
b.Sort();
Console.WriteLine("--Sorted SqlGuids--");
foreach (SqlGuid sg in b)
{
    Console.WriteLine("{0}", sg);
}
List<SqlGuid> c = a.Select(g => new SqlGuid(g)).ToList();
c.Sort();
Console.WriteLine("--Sorted SqlGuids 2--");
foreach (SqlGuid sg2 in c)
{
    Console.WriteLine("{0}", sg2);
}

Can i sort guids list and make binary search?


By : user4966375
Date : March 29 2020, 07:55 AM
hope this fix your issue Summary:
If you have a list of Value and you want to sort your list according the Guid you have two possiblities. Either you sort the list explicitly by the property you want or you implement IComparable
code :
var orderedList = list.OrderBy(i => i.Guid);
public class Value: IComparable<Value>
{
    public int CompareTo(Value other)
    {
        if(other == null)
        {
            return 1;
        }
        return Guid.CompareTo(other.Guid);
    }
    //[...]
}
var orderedList = list.OrderBy(i => i).ToList();
int index = orderedList.BinarySearch(new Value{ Guid = guidToSearchFor });
using System.IO;
using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        List<Value> list = new List<Value>
        {
            new Value() { Guid = Guid.NewGuid() },
            new Value() { Guid = Guid.NewGuid() },
            new Value() { Guid = Guid.NewGuid() },
            new Value() { Guid = Guid.NewGuid() }
        };

        var orderedList = list.OrderBy(i => i).ToList();
        int index = orderedList.BinarySearch(new Value{ Guid = list[2].Guid });
        Console.WriteLine(index);
    }
}

public class Value: IComparable<Value>
{
    public int CompareTo(Value other)
    {
        if(other == null)
        {
            return 1;
        }
        return Guid.CompareTo(other.Guid);
    }

    public Guid Guid {get;set;}
    public double Val {get;set;}
}

Sort Sequential Guids Generated by UuidCreateSequential


By : ANN Gakii
Date : March 29 2020, 07:55 AM
may help you . After researching, I can't sort the guid using the default sort or even using the default string representation from guid.ToString as the byte order is different.
to sort the guids generated by UuidCreateSequential I need to convert to either BigInteger or form my own string representation (i.e. hex string 32 characters) by putting bytes in most signification to least significant order as follows:
code :
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!");
}
shadow
Privacy Policy - Terms - Contact Us © bighow.org