Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit abfa6f3

Browse files
committed
Update char for every sorting changes
1 parent 4554177 commit abfa6f3

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

src/components/SortingAnimations.js

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,16 @@ class SortingAnimations extends React.Component {
5555
79,
5656
89
5757
],
58-
sortingRun: 0
58+
sortingRun: [0, 0]
5959
};
6060

6161
chartRef = React.createRef();
6262
chart = null;
63+
interval = null;
6364

6465
componentDidMount() {
6566
this.CreateChart();
66-
setInterval(() => this.SortData(), 500);
67+
this.interval = setInterval(() => this.SortData(), 10);
6768
}
6869

6970
CreateChart() {
@@ -83,6 +84,9 @@ class SortingAnimations extends React.Component {
8384
},
8485
options: {
8586
//Customize chart options
87+
animation: {
88+
duration: 0
89+
},
8690
legend: { display: false },
8791
title: { display: false },
8892
scales: {
@@ -101,23 +105,28 @@ class SortingAnimations extends React.Component {
101105
SortData() {
102106
let numbers = this.state.data;
103107
let run = this.state.sortingRun;
104-
if (run < numbers.length - 1) {
105-
for (let i = 0; i < numbers.length - run - 1; i++) {
106-
if (numbers[i] < numbers[i + 1]) {
107-
let temp = numbers[i];
108-
numbers[i] = numbers[i + 1];
109-
numbers[i + 1] = temp;
108+
if (run[0] < numbers.length - 1) {
109+
if (run[1] < numbers.length - run[0] - 1) {
110+
if (numbers[run[1]] < numbers[run[1] + 1]) {
111+
let temp = numbers[run[1]];
112+
numbers[run[1]] = numbers[run[1] + 1];
113+
numbers[run[1] + 1] = temp;
110114
}
115+
run[1]++;
116+
} else {
117+
run[1] = 0;
118+
run[0]++;
111119
}
112-
run++;
113-
this.setState({
114-
data: numbers,
115-
sortingRun: run
116-
});
117-
this.chart.data.labels = this.state.data;
118-
this.chart.data.datasets[0].data = this.state.data;
119-
this.chart.update();
120+
} else {
121+
clearInterval(this.interval);
120122
}
123+
this.setState({
124+
data: numbers,
125+
sortingRun: run
126+
});
127+
this.chart.data.labels = this.state.data;
128+
this.chart.data.datasets[0].data = this.state.data;
129+
this.chart.update();
121130
}
122131

123132
render() {

0 commit comments

Comments
 (0)