• ResolvedPlugin Contributor empiresafe

    (@empiresafe)


    Hi there,

    Numbers don’t sort in the correct order in this plugin, please see my attached list below. You can see how the comparison is being done digit-by-digit instead of whole numbers, is there a way to do numeric sorting like default wordpress has?

    1015
    1100
    1164
    1210
    1257
    1264
    1300
    1320
    1390
    1500
    1535
    1540
    1543
    1585
    1590
    1622
    1624
    1665
    170
    1715
    1780
    1819
    1823
    1830
    1860
    1870
    1925
    1935
    1995
    200
    2000
    2029
    2030
    2100
    2132
    2235
    2247
    225
    2285
    2310
    2332
    2335
    2343
    2400
    2490
    2510
    265
    2650
    2706
    2715
    2777
    2792
    2825
    2850
    2940
    3025
    3035
    3075
    3090
    3235
    3248
    325
    3300
    3335
    3450
    3456
    350
    3520
    3530
    355
    3594
    3600
    3640
    365
    3681
    370
    3700
    3750
    3960
    400
    4096
    4129
    4200
    4250
    430
    4320
    4400
    4490
    450
    4592
    4700
    4740
    4815
    510
    5200
    5225
    525
    550
    5590
    5700
    585
    595
    5975
    5992
    6000
    610
    620
    625
    6676
    675
    7000
    730
    740
    750
    7760
    8000
    825
    850
    880
    890
    900
    930
    950
    965
    990

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Contributor empiresafe

    (@empiresafe)

    I should also point out, this is nothing to do with the front end. This happens in the drag-and-drop screen when clicking “Sort Alphabetically”, see attached here another list of decimals that don’t get sorted correctly.
    0.41
    0.70
    0.90
    0.92
    0.94
    0.95
    1.36
    1.40
    1.43
    1.5
    1.57
    1.72
    1.75
    10
    10.00
    10.10
    10.42
    10.50
    10.59
    10.63
    10.65
    10.74
    10.94
    11.00
    11.11
    11.55
    11.67
    11.74
    12.09
    12.11
    12.20
    12.30
    12.31
    12.35
    12.40
    12.73
    12.96
    13.15
    13.18
    13.20
    13.24
    13.30
    13.89
    14.27
    14.33
    14.44
    14.47
    14.67
    14.75
    15.02
    15.04
    15.08
    15.28
    15.31
    15.42
    16.33
    16.63
    16.86
    17.04
    17.70
    18.13
    19.56
    2.06
    2.1
    2.10
    2.26
    2.27
    2.33
    2.34
    2.37
    2.43
    2.48
    2.78
    2.84
    2.87
    2.96
    20.37
    20.45
    21.10
    21.22
    22.25
    22.92
    23.10
    24.94
    25.53
    26.40
    26.67
    3.04
    3.1
    3.17
    3.2
    3.28
    3.30
    3.33
    3.55
    3.70
    3.80
    3.95
    33.54
    33.69
    36.58
    36.83
    39.49
    39.50
    4.00
    4.08
    4.14
    4.28
    4.33
    4.37
    4.4
    4.44
    4.52
    4.57
    4.66
    4.86
    4.87
    4.98
    41.56
    5.00
    5.39
    5.49
    5.56
    5.58
    5.73
    5.74
    5.75
    5.76
    5.93
    6.00
    6.09
    6.10
    6.21
    6.33
    6.35
    6.39
    6.54
    6.61
    6.62
    6.67
    6.75
    6.77
    6.82
    6.95
    7.08
    7.15
    7.29
    7.44
    7.59
    7.60
    7.62
    7.64
    7.70
    7.71
    7.74
    7.75
    7.82
    7.87
    7.91
    7.96
    7.97
    8.00
    8.04
    8.10
    8.19
    8.30
    8.33
    8.4
    8.43
    8.51
    8.52
    8.57
    8.67
    8.75
    8.82
    8.85
    8.89
    8.95
    8.97
    9.06
    9.16
    9.26
    9.44
    9.48
    9.85
    9.92

    Plugin Author Marcel Pol

    (@mpol)

    Hi, interesting bugreport πŸ™‚
    I understand the problem, and it is quite annoying, I can imagine.
    I cannot think of a solution right away. The comparison is done with variables of the string type, and a string of ‘2’ comes after ’10’.
    If I would cast averything to an int or float, it would break all text strings.

    I could see if there is a match with a regex, and if so, cast both to a float and then compare.
    I will have to think about that and try some things out. I will come back on it.

    Plugin Contributor empiresafe

    (@empiresafe)

    Hi Marcel,

    I came up with a solution that works for me for now. In script.js I changed the function customtaxorder_asc_sort being used as the custom sort function when customtaxorder_orderalpha() is called on the button press.

    // Ascending sort
    function customtaxorder_asc_sort(a, b) {
    	//return (jQuery(b).text()) < (jQuery(a).text()) ? 1 : -1;
    	//console.log (jQuery(a).text());
    	if (!isNaN(jQuery(a).text())) {
    		return jQuery(a).text()-jQuery(b).text();
    	} else {
    		return jQuery(a).text().toUpperCase().localeCompare(jQuery(b).text().toUpperCase());
    	}
    }

    This uses isNaN (not a number) to check if each value is a number before deciding which sorting algorithm to use. This solves my problem as my attributes are either numbers or letters but not both. I’m still a little unfamiliar with the .sort functionality, but I’m pretty sure this “solution” will break if it encounters attributes that are both numbers and letters.

    Plugin Contributor empiresafe

    (@empiresafe)

    Hi Marcel,

    I researched a little more on the .localecompare function going on here, and found out it was just as simple as adding some options! Simply replace the customtaxorder_asc_sort with

    function customtaxorder_asc_sort(a, b) {
    	return jQuery(a).text().localeCompare(jQuery(b).text(), undefined, {numeric: true, sensitivity: 'base'});
    }

    and it’s perfect! Can you please include this fix in future updates? I am buying the premium version of this plugin now that I have my issue permanently solved, thanks for the help!

    Please note that I took out the conversion to uppercase as the “base” setting here makes it redundant.

    • This reply was modified 8 years, 3 months ago by empiresafe.
    • This reply was modified 8 years, 3 months ago by empiresafe.
    Plugin Contributor empiresafe

    (@empiresafe)

    Just realized you don’t have a premium version of this plugin haha, I was confusing it with another plugin that I got this one to help with. If you throw this fix into the next update, I would love to give you a donation for your trouble if you have a link or Venmo!

    Plugin Author Marcel Pol

    (@mpol)

    Thank you, that should be the right fix. Thanks for taking the time to figure it out.

    The fix is applied in 2.9.3.

    THis plugin has no Pro version. You probably mistake it for the other one πŸ™‚

    If you want to donate, you can use Paypal to donate to [email protected]

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Can’t sort numbers?’ is closed to new replies.