Javascript

Messages
1,543
Reaction score
1,589
Points
1,080
Location
Belgium
So for school i'm making a mealorder website, i'm struggling with the sort tho.... Can someone help me out?

so i have this already:

"use strict";

document.addEventListener("DOMContentLoaded", init);

function init() {
let html = `<option value="id">id</option><option value="img">img</option>
<option value="book">book</option><option value="cal">calories</option>
<option value="serv">servings</option><option value="type">type</option>
<option value="price">price</option><option value="cook">cook</option>
<option value="quan">quantity</option>`;
document.querySelector('#sortby').innerHTML = html;
document.querySelector("#sortby").addEventListener("change", sortByCheck)
}



document.querySelector('input').addEventListener('keyup', search);

function search() {
let input, filter, i, txtValue, article, h3;
input = document.querySelector("input");
filter = input.value.toLowerCase();
article = document.querySelectorAll('article');
for (i = 0; i < meals.length; i++) {
h3 = article.getElementsByTagName("h3")[0];
txtValue = h3.textContent || h3.innerText;
if (txtValue.toLowerCase().indexOf(filter) > -1) {
article.style.display = "";
} else {
article.style.display = "none";
}
}
}



function sortByCheck() {
let sorters = document.querySelector('#sortby').options;
let wichsorted = document.querySelector('#sortby').selectedIndex;
let sorter = sorters[wichsorted].text;
console.log(sorter);
localStorage.getItem('mealsList');
allMeals.sort(function(a, b){return b.calories - a.calories}); // only did this to try if it works this way, it doesn't
addMeals();
}





this is the list, with meals:
it should be able to sort on every property of the list.


1545050475963.png




I already figured out to check on wich one the user wants it sorted, that works but i have no clue how to sort it then.... didn't find anything like this on the internet!
 
Messages
1,152
Reaction score
3,850
Points
735
Location
United Kingdom
What type of sorting algorithm do you want to use is the real question. And by what query/data do you want to search.
 
Messages
1,289
Reaction score
1,251
Points
650
Location
Slovenia
Javascript is the devil's language.

Use CoffeeScript or something. It's much simpler and it's basically Java, but reconstructed.

But ofc if you're in school, so you won't be allowed to use anything other than Javascript as you're probably learning about it.
 
Messages
1,543
Reaction score
1,589
Points
1,080
Location
Belgium
yes i'm in school so i'm not allowed to use anything else, but i found a solution, i can now sort on numbers, not on strings tho
 
Messages
1,543
Reaction score
1,589
Points
1,080
Location
Belgium
This is partially fixed rn, i can sort them if it are numbers, doesn't work for strings tho. I'll just be happy with the fact more then 75% works.
 
Messages
1,543
Reaction score
1,589
Points
1,080
Location
Belgium
it's just a list of meals, They each have the same properties with different values. I just need to order them by those properties
 
Messages
1,152
Reaction score
3,850
Points
735
Location
United Kingdom
here's what you can try

get it to work for sorting with strings for everything which can be done by checking against the first character of each of them and using ASCII values to determine which one will be highest and which go beneath.

Then once its working for strings, make it check for any characters in the string by doing a linear search through it, if there are no characters then it should mean it's a number and therefore you can convert it back into integer form and sort it

although I don't know much about actual javascript
 
Messages
1,543
Reaction score
1,589
Points
1,080
Location
Belgium
this is actually quite a nice idea, i'll do it this way, it will definitly work

Thank you for your response!
 

Similar threads

Top