I have a series of divs that display data on different people. The data for each person is stored in an array of objects, and there are a lot of values for each person. There is a button for each property, and I would like to be able to sort the divs in ascending or descending order of a property by clicking on a button. Each div is given an id, that corresponds to an id of the associated person in the array.
The data looks like (but it is much larger than this):
people = [
{
id: 'xde234',
height: '196',
weight: '100',
age: '34'
},
{
id: 'sbd451',
height: '176',
weight: '140',
age: '26'
},
{
id: 'loe489',
height: '156',
weight: 'NA',
age: '54'
}]
The data for each person is displayed by looping through the data, creating a div for each person, assigning their id as the id of the div, and defining the inner html of a span with each property value. The class name of the span is the same as the property. So the HTML looks likes:
<div id="peopleContainer">
<div id='xde234'>
<span class="height">196</span>
<span class="weight">100</span>
<span class="age">34</span>
</div>
<div id='sbd451'>
<span class="height">176</span>
<span class="weight">140</span>
<span class="age">26</span>
</div>
<div id='loe489'>
<span class="height">156</span>
<span class="weight">NA</span>
<span class="age">54</span>
</div>
</div>
Each button is created with an eventlistener e.g:
heightButton = document.getElementById('heightButton')
heightButton.addEventListener('click', function(){
// sortingFunction
}
I have been retrieving the divs and writing a sorting function using :
function sortingFunction(property){
peopleContainer = document.getElementById('peopleContainer')
allPeople = peopleContainer.children
}
I have 3 questions really, the main one being the first:
What is the most efficient way to sort and display this data in javascript for any property in the data, considering there will be many people and a large number of properties
How do I implement ascending and descending sort into one button, so it alternates between the two.
When a button is clicked, regardless of if it is ascending or descending, I would like all the NA values to be at the bottom, i.e. they are ignored in the sort but appended to the end.
Any help is greatly appreciated.