SortOrders Request - Sort by Date and second field

Post your requests for addons here, and discuss any addons in development here, as well.
Once an addon has completed development, it will be released in the "Addon Release" forum.

Moderator: Spunkmeyer

Postby Dale Ray » Mon Mar 07, 2005 11:06 pm

I would like to sort by the item date and time and a second field, but date is not an option when sorting by two specific fields.

I want to be able to list all events sorted by date (from oldest to newest) and within that day sorted by another field.

Is there a way to make this work?

Note - edited to clarify what I want to do.
Dale Ray
User avatar
Dale Ray
 
Posts: 1001
Joined: Sun Jan 19, 2003 6:02 pm
Location: NW Indiana

Postby Parahead » Tue Mar 08, 2005 6:28 pm

Dale Ray wrote:I would like to sort by the item date and time and a second field, but date is not an option when sorting by two specific fields.

I want to be able to list all events sorted by date (from oldest to newest) and within that day sorted by another field.

Is there a way to make this work?
It should be possible to add. The thing that I come to think of is that the submission time consists of both date *and* time (actually just seconds since 1970), so in order for this to work it would require the timefield to be splitted into these two fields. Next time I look into CDN I will try and implement this request as well as your other one about optionaly leave out the year and seconds when submitting news.
Yes, I am still around...
www.parahead.com/coranto/
User avatar
Parahead
 
Posts: 4837
Joined: Fri Jan 12, 2007 8:54 pm
Location: Stockholm - Sweden

Postby Dale Ray » Tue Mar 08, 2005 8:42 pm

This topic was split from here . I posted my first message in the wrong place. Sorry for the confusion.

Parahead wrote:
Dale Ray wrote:I would like to sort by the item date and time and a second field, but date is not an option when sorting by two specific fields.

I want to be able to list all events sorted by date (from oldest to newest) and within that day sorted by another field.

Is there a way to make this work?
It should be possible to add. The thing that I come to think of is that the submission time consists of both date *and* time (actually just seconds since 1970), so in order for this to work it would require the timefield to be splitted into these two fields. Next time I look into CDN I will try and implement this request as well as your other one about optionaly leave out the year and seconds when submitting news.


For my purpose the date and time would not have to be separated. I just want to be able to sort either chronological or reverse chronological (just like the options available now in Coranto) and by another field. In my specific case I want to be able to use the isNewDay sub and isNewCategory sub in my style. So I end up with:

Thursday, March 10
Category Heading
Item 1
Item 2
Category Heading
Item 1
Iten 2

I don't know if it would be easier to allow the user to use the built in date sorts and then sort by the SortOrders sort routines.

I remember that you had a customized version of SortOrders that sorted by four fields. Any chance of that being released?
Dale Ray
User avatar
Dale Ray
 
Posts: 1001
Joined: Sun Jan 19, 2003 6:02 pm
Location: NW Indiana

Postby Parahead » Wed Mar 09, 2005 6:21 pm

Dale Ray wrote:For my purpose the date and time would not have to be separated. I just want to be able to sort either chronological or reverse chronological (just like the options available now in Coranto) and by another field.
Just to clearify things, the second sort option only kicks in if the first one is the same between two newitems. Since the newstime is constructed of *seconds* since 1970 they would need to be submitted on the same second before the second sort would kick in. That is why I mentioned that the date and time would need to be separated. The way the sorting is done it can´t just take the newstime value straight off (unless you use the new feature in CDN to only alter date which would make the seconds the same for all items submitted that day)... I will see if I can give you a sorting routine to play with tough, OK?

Dale Ray wrote:I remember that you had a customized version of SortOrders that sorted by four fields. Any chance of that being released?
I haven´t really considered this, since there is seldom a need for being able to sort by that many fields, think it would just confuse people? Also, the one I made did some specific things (multiplied and subtracted values) with the fields before sorting them, since it was used to calculate the position of teams in a tournament.
Yes, I am still around...
www.parahead.com/coranto/
User avatar
Parahead
 
Posts: 4837
Joined: Fri Jan 12, 2007 8:54 pm
Location: Stockholm - Sweden

Postby Dale Ray » Wed Mar 09, 2005 7:02 pm

Parahead,

I hadn't thought about the fact that the second sort wouldn't kick in if the seconds weren't the same. To do what I need you have to eliminate the time from the sort completely since the events can have different time on the same day. So I guess this is more complicated than I thought, I end up needing to do this:

Sort by Date
Sort by another field
Sort by time

This is just a thought off the top of my head - to be more flexible the SortOrders could present in the settings a list of fields, a checkbox for the type of sort (alphabetic or numeric) and a check box for ascending or descending for that field. So you could

Sort by date ascending (oldest first)
Sort by another field descending numeric (say to count the most hits)
Sort by a third field alphabetic ascending

I don't know how hard it would be to construct the addon this way. I looked at the code in the addon and right now you have a sub for each sort choice in the settings. If you set up the settings the way I've described couldn't you have four sort routines (numeric ascending, number descending, alphabetic ascending, alphabetic descending) and then a master sub that 'directs the traffic')? I know that I may be completely off base here since I am not an experienced coder.

BTW, I did try to mod the code myself and hardcoded the newstime as the first field to sort on. When it didn't work it never occurred to me that the time would screw up the sort (and that the precision of the seconds being included would make it even worse).

Thanks for your efforts on this and in general.
Dale Ray
User avatar
Dale Ray
 
Posts: 1001
Joined: Sun Jan 19, 2003 6:02 pm
Location: NW Indiana

Postby Parahead » Thu Mar 10, 2005 5:54 pm

Dale Ray wrote:Parahead,

I hadn't thought about the fact that the second sort wouldn't kick in if the seconds weren't the same. To do what I need you have to eliminate the time from the sort completely since the events can have different time on the same day. So I guess this is more complicated than I thought, I end up needing to do this:

Sort by Date
Sort by another field
Sort by time
I think this will do what you want, the "sort by another field" above is the first field option you choose under the Sort Orders heading in the profile.
Code: Select all
$addon->addSortOrder('SortOrders_SortingDateThenFieldOne', 'Sort By Date Then Field Then Time');
$Subs{'SortOrders_SortingDateThenFieldOne'} = <<'END_SUB';
sub SortOrders_SortingDateThenFieldOne {
 return   map  { $_->[0] }
  sort {
   my ($SO_Second, $SO_Minute, $SO_Hour, $SO_Month_Day, $SO_Month, $SO_Year, $SO_Week_Day, $SO_IsDST) = localtime($a->[1]);
   $da = "$SO_Year" . sprintf('%.2d', $SO_Month) . sprintf('%.2d', $SO_Month_Day);
   $de = sprintf('%.2d', $SO_Hour) . sprintf('%.2d', $SO_Minute) . sprintf('%.2d', $SO_Second);
   my ($SO_Second, $SO_Minute, $SO_Hour, $SO_Month_Day, $SO_Month, $SO_Year, $SO_Week_Day, $SO_IsDST) = localtime($b->[1]);
   $db = "$SO_Year" . sprintf('%.2d', $SO_Month) . sprintf('%.2d', $SO_Month_Day);
   $df = sprintf('%.2d', $SO_Hour) . sprintf('%.2d', $SO_Minute) . sprintf('%.2d', $SO_Second);
   ($dc = lc $a->[2]) =~ s/[\W_]+//g;
   ($dd = lc $b->[2]) =~ s/[\W_]+//g;
   $da <=> $db || $dc cmp $dd || $de <=> $df }
  map { [ $_, ($_->{'newstime'})[0], ($_->{$SortOrders_Field})[0]] } @_;
 }
END_SUB



Dale Ray wrote:This is just a thought off the top of my head - to be more flexible the SortOrders could present in the settings a list of fields, a checkbox for the type of sort (alphabetic or numeric) and a check box for ascending or descending for that field. So you could

Sort by date ascending (oldest first)
Sort by another field descending numeric (say to count the most hits)
Sort by a third field alphabetic ascending

I don't know how hard it would be to construct the addon this way. I looked at the code in the addon and right now you have a sub for each sort choice in the settings. If you set up the settings the way I've described couldn't you have four sort routines (numeric ascending, number descending, alphabetic ascending, alphabetic descending) and then a master sub that 'directs the traffic')? I know that I may be completely off base here since I am not an experienced coder.
Yes, this can be done and is something I have thought about, the NewFilesAdvanced addon uses this technique you describe, with choosing fields and type of sort and this could be used in the SortOrders addon as well. The backward compatibility wouldn´t be that good though...

Also, the placement of the choices at the profile settings page isn´t very logical either, the main sorting option being quite far away from the actual choices of fields to use. This is something I have though one should fix as well...
Yes, I am still around...
www.parahead.com/coranto/
User avatar
Parahead
 
Posts: 4837
Joined: Fri Jan 12, 2007 8:54 pm
Location: Stockholm - Sweden

Postby Dale Ray » Thu Mar 10, 2005 7:13 pm

Parahead,

Thanks! That did the job.

Note: I had to remove the spaces at the end of each line after copying and pasting the code into the addon or else it caused an error. This is a forum problem so be sure to check any code you copy and paste from here.
Dale Ray
User avatar
Dale Ray
 
Posts: 1001
Joined: Sun Jan 19, 2003 6:02 pm
Location: NW Indiana

Postby LoneOwl » Fri Mar 11, 2005 3:35 am

For some reason, instead of doing the logical thing and using the html tag <code>.....they decide to put code in a table.... Perhaps some manual editting on the part of the forum admin to correct this?
User avatar
LoneOwl
 
Posts: 1465
Joined: Sun Mar 10, 2002 2:57 am
Location: That one place, you know?

Postby Parahead » Sat Mar 12, 2005 1:02 pm

Dale Ray wrote:Thanks! That did the job.
Great! :-)

LoneOwl wrote:For some reason, instead of doing the logical thing and using the html tag <code>.....they decide to put code in a table....
They in this case, isn´t that the devteam of phpBB? And I guess the graphical presentation was the number on prio, not the cut-and-paste feature?
Yes, I am still around...
www.parahead.com/coranto/
User avatar
Parahead
 
Posts: 4837
Joined: Fri Jan 12, 2007 8:54 pm
Location: Stockholm - Sweden

Postby LoneOwl » Sun Mar 13, 2005 1:13 am

Yeah, instead of making sure it would render right without all the line wrapping, to make the code look like it should, they do what they did instead. It would have actually made it easier on their part. The code tag is essentially like the pre tag, but supposed to be for code.....

But then it ties into the whole thing that html is supposed to show the structure of the document, and style sheets to pretty it up.
User avatar
LoneOwl
 
Posts: 1465
Joined: Sun Mar 10, 2002 2:57 am
Location: That one place, you know?


Return to Addon Development and Requests

Who is online

Users browsing this forum: No registered users and 0 guests

cron