scala - Play Framework how to sort collection in repeat() form helper? -
based on the play (java) documentation, let's have following example:
public class userform { public string name; public list<myclass> itmes; }
and
@helper.inputtext(userform("name")) @helper.repeat(userform("items"), min = 1) { itemfield => @helper.inputtext(itemfield) }
however, in myclass
have overridden implementation of compareto()
. have getter getsorteditems()
return list in proper sorted order.
currently, using repeat()
helper does not list of items in ordering want. is there way specify ordering repeat()
helper? or can give list
parameter? seems possible in scala.
any appreciated, thanks!
you replace list<myclass>
sorted set:
case class myclass(id: int, name: string) val sorted = new mutable.treeset[myclass]()(new ordering[myclass] { def compare(a: myclass, b: myclass): int = { ordering.int.compare(a.id,b.id) } }) sorted.add(myclass(2,"bob")) sorted.add(myclass(1,"bill")) sorted.add(myclass(3,"jane"))
i assume list contain unique instances of myclass
, set should work fine , every time add item, set make sure stays sorted.
the java version should pretty close:
import java.util.comparator; import java.util.treeset; public class myclass { public int id; public string name; } public class myclasscomparator implements comparator<myclass> { @override public int compare(myclass a, myclass b) { return integer.compare(a.id,b.id); } } treeset<myclass> sorted = new treeset<>(new myclasscomparator());
Comments
Post a Comment