S. Valmont
S. Valmont

Reputation: 1021

Sort by Integer vs. by DateTime

I'm speaking from the .NET point of view but this could extend to other languages or frameworks that use similar logic.

Is it correct to assume that when sorting objects by a DateTime property, the DateTime value is converted to Ticks (i.e., long integers) for comparison purposes? And as a result, the speed of sorting by DateTime is not much, if any, slower than sorting by integers?

Upvotes: 0

Views: 247

Answers (2)

matzone
matzone

Reputation: 5719

You can use TicksPer constants

Here is the link

Module Module1
    Sub Main()
    ' Display these constants.
    Console.WriteLine(TimeSpan.TicksPerDay)
        Console.WriteLine(TimeSpan.TicksPerHour)
    Console.WriteLine(TimeSpan.TicksPerMinute)
    Console.WriteLine(TimeSpan.TicksPerSecond)
    Console.WriteLine(TimeSpan.TicksPerMillisecond)
    End Sub
End Module

Output

864000000000
36000000000
600000000
10000000
10000

Upvotes: -1

Tomas Kirda
Tomas Kirda

Reputation: 8413

Yes, it compares ticks. Here is actual implementation:

public int CompareTo(DateTime value) {
    long valueTicks = value.InternalTicks; 
    long ticks = InternalTicks; 
    if (ticks > valueTicks) return 1;
    if (ticks < valueTicks) return -1; 
    return 0;
}

Upvotes: 2

Related Questions