Skip to content

Commit

Permalink
fix rank
Browse files Browse the repository at this point in the history
  • Loading branch information
sgratzl committed Nov 17, 2018
1 parent f689a5d commit 38653c4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
31 changes: 13 additions & 18 deletions src/provider/LocalDataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export default class LocalDataProvider extends ACommonDataProvider {
const isGroupedBy = ranking.getGroupCriteria().length > 0;
const isSortedBy = ranking.getSortCriteria().length > 0;

if (!isGroupedBy && isSortedBy && !filter) {
if (!isGroupedBy && !isSortedBy && !filter) {
// initial no sorting required just index mapping
const order = chooseByLength(this._data.length);
order.set(range(this._data.length));
Expand Down Expand Up @@ -196,34 +196,29 @@ export default class LocalDataProvider extends ACommonDataProvider {

const types = ranking.toCompareValueType();

const ordered = Array.from(groups.values()).map((g) => {
// console.time('sort');
const groupHelper = Array.from(groups.values()).map((g) => {
sortComplex(g.rows, types);
// console.timeEnd('sort');
const groupData = Object.assign({rows: g.rows.map((d) => d.r)}, g.group);
return {g, sort: ranking.toGroupCompareValue(groupData)};
});

// sort groups
sortComplex(groupHelper, ranking.toGroupCompareValueType());

let offset = 0;
return groupHelper.map(({g}) => {
//store the ranking index and create an argsort version, i.e. rank 0 -> index i
const order = chooseByLength(g.rows.length);
const index2pos = chooseByLength(this._data.length);

for (let i = 0; i < g.rows.length; ++i) {
const ri = g.rows[i].r.i;
order[i] = ri;
index2pos[ri] = i;
index2pos[ri] = offset + i;
}
const groupData = Object.assign({rows: g.rows.map((d) => d.r)}, g.group);
const orderedGroup = Object.assign({order, index2pos}, g.group);

return {o: orderedGroup, sort: ranking.toGroupCompareValue(groupData)};
offset += g.rows.length;
return Object.assign({order, index2pos}, g.group);
});

if (ordered.length === 1) {
return [ordered[0].o];
}

// sort groups
sortComplex(ordered, ranking.toGroupCompareValueType());

return ordered.map((d) => d.o);
}


Expand Down
4 changes: 4 additions & 0 deletions src/provider/sort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ function toFunction(f: {asc: boolean, v: ECompareValueType}): (a: any, b: any)=
}

export function sortComplex(arr: {sort: ICompareValue[]}[], comparators: {asc: boolean, v: ECompareValueType}[]) {
if (arr.length < 2) {
return arr;
}

const functions = comparators.map(toFunction);

switch(functions.length) {
Expand Down

0 comments on commit 38653c4

Please sign in to comment.