Array.toSorted() thinks 6 is higher than 49

Please know, the pic of data doesn't match up w/ title But anyway, so I'm trying to get it to sort the groups in various ways (Currently testing phase so it's all just in the console) Anyway, so when it comes down to the things like projects, tasks, bugs It would for example sort it (with this javascript code) like this [ 13, 15, 21, 49, 6, 6] What is the cause for this? Also, on a sidenote, if anyone knows of a way that I can shorten it, please do tell I tried to just have "let appendix" or whatever in the switch case, and make that innerHTML, value, etc but that never seemed to work wel ldue to it becoming a string
No description
No description
53 Replies
ἔρως
ἔρως3mo ago
where can we see a live example of the code?
Brightie
BrightieOP3mo ago
well, the code is on a localhost... I can't exactly throw a localhost here bc it also has php code which is directly linked into the page
ἔρως
ἔρως3mo ago
we just need the front-end code
Brightie
BrightieOP3mo ago
sure, give me a sec
ἔρως
ἔρως3mo ago
you can go to the element inspector and copy the inner html and then put the js and css and html into codepen or jsfiddle but please, reduce it as much as possible, but leave enough for us to see the problem i can see that you're sorting strings, but the code is too hard to follow
Brightie
BrightieOP3mo ago
i didnt read what you said yet sorry!
ἔρως
ἔρως3mo ago
so, your sorting issues are simple you are sorting strings javascript reads character by character, when sorting strings '7' > '42' is true, but '37' > '42' is false
Brightie
BrightieOP3mo ago
so I should basically parse them as int and it should be fine
ἔρως
ἔρως3mo ago
no
Brightie
BrightieOP3mo ago
oh
ἔρως
ἔρως3mo ago
god no ew
Brightie
BrightieOP3mo ago
:(
ἔρως
ἔρως3mo ago
🤣 reading html is the worst way to do this type of stuff
Brightie
BrightieOP3mo ago
I'll slap you for that Anyway, what would the solution be then yes I know, but I don't have a database attached yet and as its set up now ive just created random numbers everytime
ἔρως
ἔρως3mo ago
what you can do instead is to have all the in an object for example, you have an object with the title, creator, number of tasks and blah blah blah and the element
Brightie
BrightieOP3mo ago
I mean, I do have a
$users = array(
['user'=>'jhon',
'mail'=>'[email protected]',
'pass'=>'123',
'groups' =>[
[
'name'=>'Group 1',
'id'=>'1',
'creator'=>'jhon'
],
[
'name'=>'Group 2',
'id'=>'2',
'creator'=>'jhon'
]
]
],
['user'=>'jane',
'mail'=>'[email protected]',
'pass'=>'123',
'groups' =>[
[
'name'=>'Group 1',
'id'=>'1',
'creator'=>'jhon'
]
]
],
['user'=>'jhon2',
'mail'=>'[email protected]',
'pass'=>'123',
'groups' =>[
[
$users = array(
['user'=>'jhon',
'mail'=>'[email protected]',
'pass'=>'123',
'groups' =>[
[
'name'=>'Group 1',
'id'=>'1',
'creator'=>'jhon'
],
[
'name'=>'Group 2',
'id'=>'2',
'creator'=>'jhon'
]
]
],
['user'=>'jane',
'mail'=>'[email protected]',
'pass'=>'123',
'groups' =>[
[
'name'=>'Group 1',
'id'=>'1',
'creator'=>'jhon'
]
]
],
['user'=>'jhon2',
'mail'=>'[email protected]',
'pass'=>'123',
'groups' =>[
[
and so on but
ἔρως
ἔρως3mo ago
maybe an id too
Brightie
BrightieOP3mo ago
to add that manually to then replace
ἔρως
ἔρως3mo ago
yes, that, into js you can use json for that but without the email and pass
Brightie
BrightieOP3mo ago
I haven't worked with json before yeh that's fair theyre just example accounts as im testing things out first
ἔρως
ἔρως3mo ago
just groups with the name of the creator
Brightie
BrightieOP3mo ago
so hold on, should I create the json files as if they would be the databases?
ἔρως
ἔρως3mo ago
no you're not creating a json file you're throwing the json into javascript with json_encode
Brightie
BrightieOP3mo ago
but how do I get the json then? xd
ἔρως
ἔρως3mo ago
you create a new array with this with the exact structure of what you want to pass to javascript
Brightie
BrightieOP3mo ago
so, just so I understand correctly what you're saying I should create an array such as what I send in that message (but w/o the pass&mail) then when that's been made, I do array.json_encode ? I 've never worked w/ json before so yh well, I have read data from it long ago but that's different
ἔρως
ἔρως3mo ago
basically, in php, you build the data that you need for the front-end
Brightie
BrightieOP3mo ago
oh, I can convert a php array into json right yeh I looked it up while waiting xd so I write the array in php, then I do array.json_encode() n then the json is there then from there I use the data inside of the json file isntead of the html page, which was merely for testing
ἔρως
ἔρως3mo ago
something like this but you send what you would use to create each box
Brightie
BrightieOP3mo ago
aight, thankyou
ἔρως
ἔρως3mo ago
oh, im not finished that's just step 1
Brightie
BrightieOP3mo ago
alright, im here!
ἔρως
ἔρως3mo ago
step 2: render normally the cards step 3: use javascript to sort the data step 4: remove all the elements and re-add the elements again
Brightie
BrightieOP3mo ago
more steps or?
ἔρως
ἔρως3mo ago
that's literally it remember that you're sorting the array with the data, and not the elements by removing the elements and adding them all again, you've sorted everything in html as well
Brightie
BrightieOP3mo ago
I see, makes sense, thanks
ἔρως
ἔρως3mo ago
you're welcome oh, wait, i forgot one step
Brightie
BrightieOP3mo ago
yh?
ἔρως
ἔρως3mo ago
after step 2, you have to read the parent element that has the cards, and store the element into the array with the data and on step 2, you have to have a data-id on each card, with the id of each group
Brightie
BrightieOP3mo ago
alright I think I understand
ἔρως
ἔρως3mo ago
try implementing it then
Brightie
BrightieOP3mo ago
aight
ἔρως
ἔρως3mo ago
good luck
Brightie
BrightieOP3mo ago
at the add slashes, I assume you mean the path to the php file?
ἔρως
ἔρως3mo ago
no, i mean exactly what i did in the example
Brightie
BrightieOP3mo ago
what so its literally just "addslashes(json_encode)" just like that? oh didnt know it was an actual thing
ἔρως
ἔρως3mo ago
echo '<script> const data = JSON.parse("', addslashes(json_encode($data)), '")</script>'; ^ this is what i wrote it adds slashes to specific characters, which is a pretty common escape character
Brightie
BrightieOP3mo ago
alright, got it hold on, quick question the tasks & bugs would be the cumsum of tasks&bugs from all the projects in projects[] (as every group can have multiple projects) but would it be wise to nest that many layers down or should I create a seperate JSON to hold the projects and in the groups just put PROJECT[ id => 2, id =>4] etc or how should I handle that one oh wait nvm is this nested too deeply or doesn't that really matter? @ἔρως
Brightie
BrightieOP3mo ago
No description
Brightie
BrightieOP3mo ago
oh it actually looks clean enough in the console bc of the json structure
ἔρως
ἔρως3mo ago
what you pass to javascript is what you use to render the cards the exact same stuff

Did you find this page helpful?