Skip to content
This repository has been archived by the owner on Feb 19, 2022. It is now read-only.

[ENH] Add sortOrder key to sort data in descending order #322

Merged
merged 1 commit into from
Dec 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/victory-util/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export default {
);
});

const sortedData = this.sortData(data, props.sortKey);
const sortedData = this.sortData(data, props.sortKey, props.sortOrder);

return this.cleanData(sortedData, props);
},
Expand All @@ -102,9 +102,10 @@ export default {
* Sort key should correspond to the `iteratees` argument in lodash `sortBy` function.
* @param {Array} dataset: the original dataset
* @param {mixed} sortKey: the sort key. Type is whatever lodash permits for `sortBy`
* @param {String} sortOrder: the sort Order - `ascending` (default) or `descending`
* @returns {Array} the sorted data
*/
sortData(dataset, sortKey) {
sortData(dataset, sortKey, sortOrder = "ascending") {
if (!sortKey) {
return dataset;
}
Expand All @@ -114,7 +115,13 @@ export default {
sortKey = `_${sortKey}`;
}

return sortBy(dataset, sortKey);
const sortedData = sortBy(dataset, sortKey);

if (sortOrder === "descending") {
return sortedData.reverse();
}

return sortedData;
},

/**
Expand Down
17 changes: 17 additions & 0 deletions test/client/spec/victory-util/data.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,23 @@ describe("helpers/data", () => {
]);
});

it("sorts data according to sort key and sort order", () => {
const data = [
{ x: 1, y: 1, order: 2 },
{ x: 3, y: 3, order: 1 },
{ x: 2, y: 2, order: 3 }
];

const returnData = Data.getData({ data, sortKey: "order", sortOrder: "descending" });

expect(returnData).to.eql([
{ _x: 2, x: 2, _y: 2, y: 2, order: 3, eventKey: 0 },
{ _x: 1, x: 1, _y: 1, y: 1, order: 2, eventKey: 1 },
{ _x: 3, x: 3, _y: 3, y: 3, order: 1, eventKey: 2 }

]);
});

// Ensures previous VictoryLine api for sortKey prop stays consistent
it("sorts data according to evaluated sort key when sort key is x or y", () => {
const data = [
Expand Down