Browse Source

Entries are now sorted by month if the field is present.

Excellent patch from Jan Geldmacher.
hg/35a367e65a464cf6cdf277c73f977a6828b723fe/bookmarks/master
Martin Monperrus 14 years ago
parent
commit
d3a4b8cf80
  1. 57
      bibtexbrowser.php
  2. 62
      test/input/month-sort.bib

57
bibtexbrowser.php

@ -1420,6 +1420,59 @@ function _DefaultBibliographySections() {
return $function();
}
/** Compares two instances of BibEntry by month
* @author Jan Geldmacher
*/
function compare_bib_entry_by_month($a, $b)
{
// this was the old behavior
// return strcmp($a->getKey(),$b->getKey());
//bibkey which is used for sorting
$sort_key = 'month';
//desired order of values
$sort_order_values = array('jan','january','feb','february','mar','march','apr','april','may','jun','june','jul','july','aug','august','sep','september','oct','october','nov','november','dec','december');
//order: 1=as specified in $sort_order_values or -1=reversed
$order = -1;
//first check if the search key exists
if (!array_key_exists($sort_key,$a->fields) && !array_key_exists($sort_key,$b->fields)) {
//neither a nor b have the key -> we compare the keys
$retval=strcmp($a->getKey(),$b->getKey());
}
elseif (!array_key_exists($sort_key,$a->fields)) {
//only b has the field -> b is greater
$retval=-1;
}
elseif (!array_key_exists($sort_key,$b->fields)) {
//only a has the key -> a is greater
$retval=1;
}
else {
//both have the key, sort using the order given in $sort_order_values
$val_a = array_search(strtolower($a->fields[$sort_key]), $sort_order_values);
$val_b = array_search(strtolower($b->fields[$sort_key]), $sort_order_values);
if (($val_a == FALSE && $val_b == FALSE) || ($val_a == $val_b)) {
//neither a nor b are in the search array or a=b -> both are equal
$retval=0;
}
elseif (($val_a == FALSE) || ($val_a < $val_b)) {
//a is not in the search array or a<b -> b is greater
$retval=-1;
}
elseif (($val_b == FALSE) || (($val_a > $val_b))){
//b is not in the search array or a>b -> a is greater
$retval=1;
}
}
return $order*$retval;
}
/** the default sections */
function DefaultBibliographySections() {
return
@ -2040,8 +2093,8 @@ class DefaultContentStrategy {
</tr>
<?php
}
// sort by keys, enable a nice sorting as Dupont2008a, Dupont2008b, Dupont2008c
krsort($entries);
// sort by month if present, otherwise by keys (Dupont2008a, Dupont2008b, Dupont2008c)
uasort($entries, "compare_bib_entry_by_month");
foreach ($entries as $bib) {
if ($display->isDisplayed($index)) {
$bib->setAbbrv($refnum--);

62
test/input/month-sort.bib

@ -0,0 +1,62 @@
@misc{e2,
title= "Title 2 (April)",
year= 2011,
month = apr
}
@misc{e1,
title= "Title 1 (January)",
year= 2011,
month = jan
}
@misc{e3,
title= "Title 3 (December)",
year= 2011,
month= dec
}
@misc{f2b,
title= "Title 2 (f2b)",
year= 2010
}
@misc{f1,
title= "Title 1 (f1)",
year= 2010
}
@misc{f2,
title= "Title 2 (f2)",
year= 2010
}
@misc{f2a,
title= "Title 2 (f2a)",
year= 2010
}
%% now if we mix both
@misc{g1,
title= "Title (January)",
year= 2009,
month = jan
}
@misc{g2,
title= "Title (October)",
year= 2009,
month= oct
}
@misc{g2009b,
title= "Title (2009b)",
year= 2009
}
@misc{g2009a,
title= "Title 1 (2009a)",
year= 2009
}
Loading…
Cancel
Save