Skip to content
Permalink
Browse files

Added todo item sorting!

  • Loading branch information...
reddit-coding-exp committed Feb 25, 2019
1 parent 790ce64 commit 1ebc133bf2d68ce6fb83c8553ccec933021505a7
Showing with 69 additions and 12 deletions.
  1. +30 −11 package-lock.json
  2. +39 −1 src/App.tsx

Some generated files are not rendered by default. Learn more.

@@ -111,8 +111,9 @@ class App extends Component<AppProps, TodoList> {
};

const newTodoItemArray = [...this.state.todoItems, newTodoItem];
const sortedItems = bubbleSort(newTodoItemArray)

this.setState({ todoItems: newTodoItemArray, currentText: "" });
this.setState({ todoItems: sortedItems, currentText: "" });
};

renderItems = () => {
@@ -286,3 +287,40 @@ class TodoFooterSection extends Component<TodoFooterSectionProps> {
}

export default App;








// Create our own bubble sort

function bubbleSort(numbersToSort: TodoItemData[]){
const copiedNumbers = [...numbersToSort]

for(let i = 0; i < copiedNumbers.length; i++){

for(let j = 0; j < copiedNumbers.length - 1; j++){
const n1 = copiedNumbers[j]
const n2 = copiedNumbers[j+1]

if ( n1.text <= n2.text){
continue
} else {
copiedNumbers[j] = n2
copiedNumbers[j + 1] = n1
}
}

}

return copiedNumbers
}






0 comments on commit 1ebc133

Please sign in to comment.
You can’t perform that action at this time.