I am trying to sort products in correct order in Checkout cart ; while It is correct for different products but when the case for products having this form
'BrandName' ProductName 1,25 kg
I got flase order like this
'BrandName' ProductName 1,25 kg
'BrandName' ProductName 10,0 kg
'BrandName' ProductName 15,0 kg
'BrandName' ProductName 2,5 kg
I want to correct the order like this
'BrandName' ProductName 1,25 kg
'BrandName' ProductName 2,5 kg
'BrandName' ProductName 10,0 kg
'BrandName' ProductName 15,0 kg
without affecting the order of The other products sorted Alphabetically. This is a snippet of code
Basically, itâs treating your numbers as strings. âaâ comes before âbâ, âappleâ comes before âbananaâ because âaâ comes before âbâ.
â1â comes before â2â, so â10â comes before â2â as well.
If you want Numerical Order, youâll have to convert your strings into numbers.
Thanks but the problem I have to order both those with numerical values and thoses not
More detailed example
I need to have this result
Brand 1 âAProduct Name 1â
Brand 1 âBProduct Name 2â
Brand 1 âCProduct Name 3 1,25 kgâ (It contains 1,25 the name)
Brand 1 âCProduct Name 3 2,5 kgâ
Brand 1 âCProduct Name 3 5,0 kgâ
If that whole thing is part of a single string that isnt part of that CONCAT (I donât know your database structure, so I dont know what thatâs concating)⌠I dont know how youâd be able to sort by it numerically; it would be extremely complex for a SQL-like to handle that.
Though youâve highlighted a potential danger there already - a numeric array key must be a whole number.
If you know all the values ahead of time, yes, this works, but if youâre going to make an array of the options, why are you using a database of items in the first place?
You might be able to map the values through a regex, but youâd have to be sure all the values are using the same measures and structureâŚ
(The but being âas long as your values are all in less than 1-100th termsââŚ)
And again, if you know all the values in advance, this is trivial. if you dont thoughâŚ
âRegex for (\d*(,\d*)?â ⌠okay, as long as none of your product names include numbers.
âYou could regex for (\d*(,\d*)? kg)â Sure, if all of the values youâre sorting by are âsomething kgâ.
⌠etc etc.
If you can guarantee the data form, we can bend over backwards to sort something out.
Or you could⌠yaknow⌠put the numbers into a separate field in the table and just sort by those like a sane person.