Item10258: SEARCH order=created and order=formfield(date) doesn't work as expected

Priority: Urgent
Current State: Closed
Released In: 1.1.3
Target Release: patch
Applies To: Engine
Reported By: MartinKaufmann
Waiting For: Main.KennethLavrsen
Last Change By: KennethLavrsen
In Foswiki 1.1.2 a SEARCH with order="created" returns a list with randomly (to me) ordered results (order="modified" works OK). PaulHarvey mentioned that the unit tests for this particular case are commented out.

-- MartinKaufmann - 14 Jan 2011

Maybe related to the fact that order="formfield()" does not work with date fields either. Instead dates are sorted alphabetically.

Can you check if your created dates are sorted like it was a text string?

-- KennethLavrsen - 26 Jan 2011

I don't think it's trying to sort the dates as text strings:
screenshot 2011-01-26 15-01-47.png-- MartinKaufmann - 26 Jan 2011

I have a fix coded which both fixes created and formfield case (where formfield is a date).

Need a little more testing before I check it in.

-- KennethLavrsen - 02 Feb 2011

@KennethLavrsen: any chance we might see a patch or commit soon? This is really borking up my dashboard views frown, sad smile

On a side note, how difficult would it be to support multiple sort axes? Something like this: order = "created, formfield(FieldOne), formfield(FieldTwo)"

I've been meaning to file an enhancement about that, if someone hasn't already.

-- AaronFuleki - 16 Feb 2011

That enhancement request is SupportMultiKeySorting. Status unclear.

As mentioned in that topic, only specifying the form field will not do, you should also be able to pass the type of content: text, number, date.

-- ArthurClemens - 17 Feb 2011

Wow, there's some great discussion in SupportMultiKeySorting - I hope something happens there!

Back on topic: what's the status, Kenneth?

...tempting REM reference, there. wink

-- AaronFuleki - 17 Feb 2011

I am working on the unit tests at the moment.

They fail with my fix but I cannot make the same test fail in the real world so I am working on figuring out why it fails

-- KennethLavrsen - 18 Feb 2011

I figured out what went wrong. And I have fixed the immediate problem. But I may later want to also make the timeParse code more robust.

Unit tests are also updated.

If you want to try the code look for lib/Foswiki/Search/ and find the sub called _compare.

Here is the new sub

sub _compare {
    my $x = shift;
    my $y = shift;
    ASSERT( defined($x) ) if DEBUG;
    ASSERT( defined($y) ) if DEBUG;
    if ( $x =~ /$NUMBER/o && $y =~ /$NUMBER/o ) {
        # when sorting numbers do it largest first; this is just because
        # this is what date comparisons need.
        return $y <=> $x;

    my $datex = undef;
    my $datey = undef;
    # parseTime can error if you give it a date out of range so we skip
    # testing if pure number
    # We skip testing for dates the first character is not a digit
    # as all formats we recognise as dates are  
    if ( $x =~ /^\d/ && $x !~ /$NUMBER/o &&
         $y =~ /^\d/ && $y !~ /$NUMBER/o ) {
        $datex = Foswiki::Time::parseTime($x);
        $datey = Foswiki::Time::parseTime($y) if $datex;

    if ( $datex && $datey ) {
        return $datey <=> $datex;
    else {
        return $y cmp $x;

-- KennethLavrsen - 18 Feb 2011

ItemTemplate edit

Summary SEARCH order=created and order=formfield(date) doesn't work as expected
ReportedBy MartinKaufmann
Codebase 1.1.2
SVN Range
AppliesTo Engine
Priority Urgent
CurrentState Closed
WaitingFor KennethLavrsen
Checkins distro:2f5346e5abb7 distro:830c907f8e4b
TargetRelease patch
ReleasedIn 1.1.3
Topic revision: r12 - 16 Apr 2011, KennethLavrsen
The copyright of the content on this website is held by the contributing authors, except where stated elsewhere. See Copyright Statement. Creative Commons License    Legal Imprint    Privacy Policy