Reverse the order returned by the following function in .bst latex file or create another function that reverses the order it gets:
FUNCTION {bib.sort.order}
{ sort.label
" "
*
year field.or.null sortify
*
" "
*
title field.or.null
sort.format.title
*
#1 entry.maxsubstring
'sort.key$ :=
}
Assistant
I'll create a modified version of the function that reverses the order of sorting. In BibTeX style files, the sorting function determines how entries are ordered in the bibliography. Here's a new function that will reverse the original order:
FUNCTION {reverse.bib.sort.order}
{ sort.label
" "
*
year field.or.null sortify
swap substring :=
}
Actually, the above function doesn't properly reverse the order. Let me provide a better solution. There are two main approaches:
User
None of this worked. Just write a new function that sorts in chronological order
Assistant
I'll create a new function that sorts entries in chronological order (oldest to newest). This is a straightforward approach:
FUNCTION {chronological.sort.order}
{ sort.label
" "
*
year field.or.null sortify
duplicateempty
{ pop "0000" } % For entries without year, put them at the beginning
{ skip }
if
*
" "
*
title field.or.null
sort.format.title
*
#1 entry.max substring :=
}
This function will:
User
Good, now do it in reverse chronological order
Assistant
Here's a function that sorts entries in reverse chronological order (newest to oldest):
FUNCTION {reverse.chronological.sort.order}
{ sort.label
" "
*
year field.or.null sortify
duplicateempty
{ pop "9999" } % For entries without year, put them at the end
{
% Negate the year to reverse chronological order
duplicate
#1 #4 substring % Extract up to 4 digits of year
#9999 swap - int.to.str
*
" "
*
title field.or.null
sort.format.title
*
#1 entry.max
'sort.key$ :=
}
This function will:
User
Now I get hundreds of :
"2021" is a string literal, not an integer, for entry
Assistant
*
" "
*
title field.or.null
sort.format.title
*
#1 entry.max
′sort.key
Create a new function that produces a reversed sort key
Modify the existing function to negate or invert values
Here's a proper solution that reverses the chronological order (newest publications first), which is typically what's desired:
FUNCTION {reverse.bib.sort.order}
{ sort.label
" "
*
year field.or.null sortify
% Negate the year to reverse chronological order
duplicateempty
{ pop "9999" } % For entries without year, put them at the end
{ #1 #4 substring % Extract up to 4 digits
duplicateempty
{ pop "9999" }
{ "-" swap * } % Add a negative sign to reverse
if
}
if
" "
*
title field.or.null
sort.format.title
*
#1 entry.maxsubstring
'sort.key :=
}
Alternatively, if you want to completely reverse the order produced by the original function (which would put Z authors before A authors and older publications before newer ones), you could do:
FUNCTION {reverse.sort.order}
{ bib.sort.order % Call the original sort function to get a sort key
duplicate
empty
{ pop "" }
{
% Create a complementary string by replacing each character with its complement
% This effectively reverses lexicographic sorting
duplicatetext.length 'len :=
#0 'i :=
""
{ i len < }
{
swapduplicate i #1 substringchr.to.int
#255 swap−int.to.chr
swap *
i #1 + 'i :=
}
while
}
if′sort.key :=
}
If you simply want to use the existing function but have the entries appear in reverse order, you could also modify your .bst file to reverse the final sorted list after sorting, but that would require changes to the ITERATE and output functions.
Would you like me to explain more about how these functions work, or provide a different approach?
′sort.key
Start with the author label (sort.label)
Append the year (maintaining chronological order)
For entries without a year, use "0000" to place them at the beginning
Append the title (for entries with the same author and year)
To use this function:
Replace the existing bib.sort.order function with this one, or
Add this as a new function and change your .bst file to call chronological.sort.order instead of bib.sort.order in the sorting section
For the sorting to take effect, you'll need to ensure that your .bst file is actually using this function during the sort process. Typically, there will be a section like:
ITERATE {chronological.sort.order}
SORT
If you're still having issues, it might help to see more of your .bst file, particularly the sorting section.
% Subtract from 9999 to invert
}
if
substring
Start with the author label (sort.label)
Invert the year value by subtracting it from 9999, which effectively reverses the chronological order
For entries without a year, use "9999" to place them at the end
Append the title (for entries with the same author and year)
To use this function:
Add this as a new function to your .bst file
Modify the sorting section to use this function instead of the original
The sorting section in your .bst file should look something like:
ITERATE {reverse.chronological.sort.order}
SORT
This will result in the most recent publications appearing first in your bibliography, followed by older ones, with entries missing year information at the end.
I see the issue now. The function is treating the year as a string, but I tried to perform integer operations on it. Let me provide a corrected version that properly reverses chronological order without generating those errors:
FUNCTION {reverse.chronological.sort.order}
{ sort.label
" "
*
year field.or.null sortify
duplicateempty
{ pop "0000" } % For entries without year, put them at the end
{
% Create a complementary string for the year to reverse chronological order
% For example, "2021" becomes "7978" (9-digit for each position)
duplicate text.length 'len :=
#0 'i :=
""
{ i len < }
{
duplicate i #1 substringduplicate "0" =
{ pop "9" }
{ duplicate "1" =
{ pop "8" }
{ duplicate "2" =
{ pop "7" }
{ duplicate "3" =
{ pop "6" }
{ duplicate "4" =
{ pop "5" }
{ duplicate "5" =
{ pop "4" }
{ duplicate "6" =
{ pop "3" }
{ duplicate "7" =
{ pop "2" }
{ duplicate "8" =
{ pop "1" }
{ duplicate "9" =
{ pop "0" }
{ skip }
if
}
if
}
if
}
if
}
if
}
if
}
if
}
if
}
if
}
if
swap * % append to result
i #1 + 'i :=
}
while
}
if
*
" "
*
title field.or.null
sort.format.title
*
#1 entry.max substring′sort.key :=
}
However, there's a simpler solution that should work more reliably:
FUNCTION {reverse.chronological.sort.order}
{ sort.label
" "
*
year field.or.null sortify
duplicateempty
{ pop "0000" } % For entries without year, put them at the end
{
% Simply add a minus sign to reverse sort order
"-" swap *
}
if
*
" "
*
title field.or.null
sort.format.title
*
#1 entry.max substring′sort.key :=
}
This second version is much simpler and should work in most BibTeX implementations. It prepends a minus sign to the year, which will cause newer years to sort before older years.
If this still doesn't work, here's an even simpler approach that should definitely work:
FUNCTION {reverse.chronological.sort.order}
{ sort.label
" "
*
year empty
{ "0000" }
{ "9999" year field.or.null sortify #5 #4 substring - }
if
*
" "
*
title field.or.null
sort.format.title
*
#1 entry.max substring′sort.key :=
}
This uses a different approach to create the reverse order and should avoid string vs. integer issues.