Bug 5854 – Built-in array sort doesn't sort SysTime correctly

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2011-04-18T11:23:00Z
Last change time
2014-06-17T21:11:36Z
Keywords
wrong-code
Assigned to
nobody
Creator
Jesse.K.Phillips+D

Comments

Comment #0 by Jesse.K.Phillips+D — 2011-04-18T11:23:02Z
This could be related to Issue 5853, Sorting SysTime: overlapping array copy. But I didn't see anything obviously wrong with SysTime. import std.datetime; void main() { auto arr = [ SysTime(DateTime(2011,4,4)), SysTime(DateTime(2011,3,22)) ]; auto ans = [ SysTime(DateTime(2011,3,22)), SysTime(DateTime(2011,4,4)) ]; arr.sort; assert(arr == ans); }
Comment #1 by kennytm — 2011-04-18T12:30:54Z
It does sort the array, but not correctly, e.g. ----------------------------------------- import std.datetime, std.stdio; void main() { auto arr = [ SysTime(DateTime(2011,4,4)), SysTime(DateTime(2011,1,2)), SysTime(DateTime(2011,2,9)), SysTime(DateTime(2011,3,22))]; writeln(arr); arr.sort; writeln(arr); } ----------------------------------------- prints [2011-Apr-04 00:00:00, 2011-Jan-02 00:00:00, 2011-Feb-09 00:00:00, 2011-Mar-22 00:00:00] [2011-Jan-02 00:00:00, 2011-Apr-04 00:00:00, 2011-Mar-22 00:00:00, 2011-Feb-09 00:00:00]
Comment #2 by kekeniro2 — 2012-10-17T19:21:30Z
The cause is in Phobos library.(std.datetime) Struct SysTime has some opCmp, but lacks the required one to overload. http://dlang.org/arrays.html#array-properties > For the .sort property to work on arrays of structs or unions, the struct or union definition must define the function: int opCmp(ref const S) const. The type S is the type of the struct or union. This function will determine the sort ordering. # I have no idea whether DMD could print an error or not.
Comment #3 by bugzilla — 2014-06-17T19:43:03Z
Comment #4 by github-bugzilla — 2014-06-17T21:11:36Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/4d1dd273adfc2a81b425c46d92364db70bd01fd6 fix Issue 5854 - Built-in array sort doesn't sort SysTime correctly https://github.com/D-Programming-Language/dmd/commit/539db52b84bfc85a159fc71fd75a8b3c3a4e0c17 Merge pull request #3670 from dcarp/fix5854 fix Issue 5854 - Built-in array sort doesn't sort SysTime correctly