-
Notifications
You must be signed in to change notification settings - Fork 896
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Not sorting negative numbers correctly. Reproduced on listjs.com #562
Comments
I've changed account. This is my account now. Issue is still present. |
Hello, I've figured this out. I resolved the problem by creating my own sort function and telling ListJS to use that. Below is the code that works for ME, but may not for you. So modify it as necessary.
Feel free to close this out, I wasn't sure if anybody else wanted to look at it first. Thanks. |
I am having this exact problem in 2022, and can't get your code to work. Could you help? In the code above, sortOrder is not defined. Do you simply replace it with "asc" or "desc"? And why would you want to define this at all? The user needs to be able to choose either asc or desc, and does this not fix the sort order? What does "return 1", "return -1" and "return 0" mean? Are you just using "return 1" for "return: true"? I have never seen "return -1" before, so don't understand this. Finally, the values below are what I console.log out from "before" and "after" when running the code on a table with these values: [1.71, -3.95, -2.09, 3.39, 12.36, -1.28]. Any idea what's happening: before: NaN Would very much appreciate some pointers. Cheers. |
Did you manage to figure this out? |
Sorry about the late reply @Vemb, I did not see your issue until now.
|
I'm experiencing this same issue on my CMS market share report; positive values get sorted properly when in descending order (300 - 200 - etc.), but when sorting in ascending order, -0.5% comes higher than -20%, which is not what it should be. Feel free to test it out on my site, you can see all the code there too. |
Here is another simple codepen which shows the sorting issue with negative numbers. |
Line 1879 in 0c94716 Something like this to detect if we are comparing two numeric values maybe? I think the function naturalCompare(a, b) {
var lengthA = (a += '').length;
var lengthB = (b += '').length;
var aIndex = 0;
var bIndex = 0;
// if we are comparing two numbers
if ( a != "" && b != "" && !isNaN(a) && !isNaN(b))
{
return Math.sign(parseFloat(a) - parseFloat(b));
}
while (aIndex < lengthA && bIndex < lengthB) {
var charCodeA = a.charCodeAt(aIndex);
var charCodeB = b.charCodeAt(bIndex);
.... |
I've submitted a PR that I think should fix this issue: #779 |
Hello, listjs is not sorting negative numbers correctly. You can reproduce the code on your own website. Take a look at: http://listjs.com/examples/add-get-remove/
Click "Age" column. As you can see it goes from -23 -> -132 -> 26.
This is wrong, since -132 is smaller than -23, it should be going -132 -> -23 -> 26, etc.
The text was updated successfully, but these errors were encountered: