<div class="transaction-row" id= "transaction-row">
<div class="name">Name 1</div>
<div class="transaction-type"> Category 1</div>
<div class="date">Date 1</div>
<div class="amount"> 1738</div>
<div class="exp-or-inc">Exp or Inc </div>
</div>
<div class="transaction-row" id= "transaction-row">
<div class="name">Name 2</div>
<div class="transaction-type"> Category 2</div>
<div class="date">Date 2</div>
<div class="amount"> 50</div>
<div class="exp-or-inc">Exp or Inc </div>
</div>
The objective here is to arrange all "transaction rows" based on the value in the child element with the class name "amount" using JavaScript. I have been exploring methods to sort multiple DOM elements by child elements with the sort() function without success.
Update:
Below is my JavaScript code:
let transEntries = document.getElementsByClassName("transaction-row");
let sortedEntries = [].slice.call(transEntries);
sortedEntries.sort((a,b)=> {
if(a.children[3] === b.children[3]) {
return 0;
} else {
return (a.children[3] < b.children[3]) ? -1 : 1;
}
});
All I require is to log the following in the console:
<div class="amount"> 50</div>
<div class="amount"> 1738</div>
I should be able to rearrange the elements once they are stored in an array.