Sort homes by price in ascending order:
homes.sort(function(a, b) {
return parseFloat(a.price) - parseFloat(b.price);
});
Or after ES6 version:
homes.sort((a, b) => parseFloat(a.price) - parseFloat(b.price));
Some documentation can be found here.
For descending order, you may use
homes.sort((a, b) => parseFloat(b.price) - parseFloat(a.price));
Move them to an array, sort that array, and then use that array for your purposes. Here's a solution:
var maxSpeed = {
car: 300,
bike: 60,
motorbike: 200,
airplane: 1000,
helicopter: 400,
rocket: 8 * 60 * 60
};
var sortable = [];
for (var vehicle in maxSpeed) {
sortable.push([vehicle, maxSpeed[vehicle]]);
}
sortable.sort(function(a, b) {
return a[1] - b[1];
});
//[["bike", 60], ["motorbike", 200], ["car", 300],
//["helicopter", 400], ["airplane", 1000], ["rocket", 28800]]
Once you have the array, you could rebuild the object from the array in the order you like, thus achieving exactly what you set out to do. That would work in all the browsers I know of, but it would be dependent on an implementation quirk, and could break at any time. You should never make assumptions about the order of elements in a JavaScript object.
var objSorted = {}
sortable.forEach(function(item){
objSorted[item[0]]=item[1]
})
In ES8, you can use Object.entries()
to convert the object into an array:
const maxSpeed = {
car: 300,
bike: 60,
motorbike: 200,
airplane: 1000,
helicopter: 400,
rocket: 8 * 60 * 60
};
const sortable = Object.entries(maxSpeed)
.sort(([,a],[,b]) => a-b)
.reduce((r, [k, v]) => ({ ...r, [k]: v }), {});
console.log(sortable);
In ES10, you can use Object.fromEntries()
to convert array to object. Then the code can be simplified to this:
const maxSpeed = {
car: 300,
bike: 60,
motorbike: 200,
airplane: 1000,
helicopter: 400,
rocket: 8 * 60 * 60
};
const sortable = Object.fromEntries(
Object.entries(maxSpeed).sort(([,a],[,b]) => a-b)
);
console.log(sortable);
Best Answer
There may be much easier and straight forward solutions:
I am using Jasper Reports Studio (Eclipse Plugin).
did not finally work for me :-( ... (see 1. comment below my answer - may be a bug) you just can check [x] Data Pre Sorted in the Crosstab Properties. Of course this only works if you do not need another ordering somewhere else in the report based on the pre-sorted result set.
using an invisible crosstab group header, which is rather tricky:
Row Group1
or similar as is for nowTotal Position
toNone
(we do not want to have a total column per row/column generated)name="invisible sort column ..."
if you are using group totals on your group (
Total Position
!=None
) then you have to basically move these totalling elements/settings to the first dummy sort group, because otherwise the totals will be no totals anymore (= per 2nd group totals) displayed after each group column/row, e.g. (here only shown with column group, but row group follows the same principle)easiest may be to do this in the XML similar to this transformation (do not forget to move the
totalPosition=...
andcolumnTotalGroup=...
attributes and to change the sum if applicable to your scenario$V{SomeSum_..._ALL}
):=>
(may be skipped:) remove unnecessarily generated
<crosstabCell ... column/rowTotalGroup="...">
cells$F{ORDER_FOR_X}
Value Class Name
tojava.lang.Integer
or whatever fits for your values here (have a look at your dataset which type is assigned there if your are using it via some column)add some variable expression to the
Order By Expression
of the original group, e.g.$V{ORDER_FOR_X}
$V{...}
is the trick, do not use$F{...}
!meaning if you can provide some field that defines the sort and relates to your to-be-sorted column value, e.g. (Oracle SQL)
otherwise you can of course use something else here as well
if you should get some
you can simply change the expression to
$V{ORDER_FOR_X} == null ? 0 : $V{ORDER_FOR_X}
which should do the trickset all high/width fields of the dummy group to
0
and the text fieldsPrint When Expression
tofalse
using an order by based on measure totals (as described in the bottom link)
using a custom Java Comparator class (as described in the answer from Pieter VN 2011-11-16)
further maybe helpful links I found: