+ }
+
+ my @rows = @{$units};
+ my $grouped = 1; # race headers
+ if (exists $get{order}) {
+ $grouped = 0;
+ $get{order} ||= '';
+ if ($get{order} eq 'name') {
+ @rows = sort {$a->{name} cmp $b->{name}} @rows;
+ }
+ elsif ($get{order} eq 'hp') {
+ $_->{order} = (
+ $_->{hp}*1.01 + $_->{armor} + $_->{shield} + $_->{size}/1024,
+ ) for @rows;
+ }
+ elsif ($get{order} eq 'cost') {
+ $_->{order} = (
+ $_->{gas}*1.5 + $_->{min} + $_->{pop}/8 + $_->{build}/256/8
+ ) for @rows;
+ }
+ elsif ($get{order} eq 'size') {
+ $_->{order} = (
+ $_->{pop}*16 + ($_->{size} // $_->{suit}) + $_->{cargo}/8
+ + $_->{hp}/512 + $_->{min}/8192
+ ) for @rows;
+ }
+ elsif ($get{order} eq 'attack') {
+ $_->{order} = $_->{hp} / 1024 + $_->{shield} / 1008 + max(
+ map {
+ ($_->{dps} ? $_->{dps}->[-1] :
+ ($_->{damage} + $_->{upgrade} * 3)
+ * ($_->{count} // 1) / ($_->{cooldown} // 1)
+ )
+ * ($_->{splash} ? 1.01 : 1)
+ * ($_->{type} eq 'implosive' ? .96 : 1)
+ * ($_->{type} eq 'explosive' ? .98 : 1)
+ } @{ $_->{attack} }
+ ) for @rows;
+ }
+ elsif ($get{order} eq 'speed') {
+ $_->{order} = (
+ ($_->{upgraded}->{speed} // $_->{speed}*1.01)
+ + $_->{sight}/1024 + $_->{detect}/2048
+ ) for @rows;
+ }
+ @rows = sort {$a->{order} <=> $b->{order}} @rows if exists $rows[0]->{order};
+ }
+
+ my ($race, $cat) = ('', '');
+ for (@rows) {
+ if ($grouped) {
+ say sprintf '<tbody id="%s"><tr class="race"><th colspan="20"><h2>%s</h2>',
+ $race = $_->{race}, ucfirst $race
+ unless $race eq $_->{race};
+ }
+ else {
+ $_->{cat} = $_->{race};
+ }