How to sort entries array (from Vasya)

83 views
Skip to first unread message

Chris H

unread,
Oct 25, 2018, 5:26:44 PM10/25/18
to mementodatabase
I was trying to sort the entries array using standard JavaScript code:

var entries = lib().entries();
entries
.sort(function(a,b){return b.field("Miles") - a.field("Miles")});


But unexpectedly this gave a Java (not Script!) error message:
org.mozilla.javascript.EvaluatorException: Cannot convert function to interface java.util.Comparator since it contains methods with different names

I reported this as a bug to Vasya, but he provided a solution (or work-around) :
comparator = { compare: function(a,b) {  return b.field("Miles") - a.field("Miles") ;  }}
entries.sort(comparator);

So I fixed my original code like so:

var entries = lib().entries();
entries
.sort({compare:function(a,b){return b.field("Miles") - a.field("Miles")}});


Hopefully others may find this trick useful!

Bill Crews

unread,
Oct 25, 2018, 6:23:49 PM10/25/18
to Chris H, mementodatabase
In case no one else does, THANKS! It's great to keep the forum alive with information from everyone.
Reply all
Reply to author
Forward
0 new messages