Template talk:Number table sorting

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia
Note. Adding data-sort-type="number" to the relevant column header solves many numerical sorting problems. See Help:Sorting.

Centralized discussion for all issues surrounding sortkeys for wikitables created here: Template talk:Sort. ~ trialsanderrors 09:02, 5 April 2007 (UTC)[reply]

Bizarre interaction with age template[edit]

See Wikipedia:Village pump (technical)#Bizarre interaction between age and nts templates. —Steve Summit (talk) 01:25, 29 November 2008 (UTC)[reply]

Indian numbering system[edit]

I am trying to translate this template for an Indian language which uses the Indian numbering system. Anyone has an idea how to modify this template for that?
Thanks Abhijit Sathe (talk) 21:43, 13 July 2009 (UTC)[reply]

Edit request from 134.253.26.6, 15 November 2010[edit]

{{edit protected}} Many pages would like to use this template with a non-numeric prefix, e.g. a less-than sign or a greater-than sign (see List of European capital cities by area). One option is to just use {{sort}} for the entire table, but it would be helpful if we could add a |prefix= option to this template. To do this, change </span>{{formatnum:{{{1}}}}}}} to </span>{{{prefix|}}}{{formatnum:{{{1}}}}}}}, which will insert the optional prefix between the "no display" and actual display parts. Thanks! 134.253.26.6 (talk) 16:57, 15 November 2010 (UTC)[reply]

That seems sensible and fairly uncontroversial, so done. Please could you update the documentation? — Martin (MSGJ · talk) 17:10, 15 November 2010 (UTC)[reply]
Documentation updated. Thank you. 134.253.26.6 (talk) 18:03, 15 November 2010 (UTC)[reply]

Requested edit: Making number formatting optional[edit]

I'm making some edits to a large table containing lots of dates and find the formatnum magic word is causing commas in the years, which looks odd.

I've just updated the sandbox (revision 424380852) with a new argument {{{noformat}}} that will optionally suppress that formatnum call. Could someone effect that edit to the template itself, please?

The specifics of the edit are changing

{{formatnum:{{{1}}}}}}}

to

{{#ifeq:{{{noformat|}}}|yes|{{{1}}}|{{formatnum:{{{1}}}}}}}}}

, the rest of the template is merely a copy/paste of the current revision. Thanks! — OwenBlacker (Talk) 15:52, 16 April 2011 (UTC)[reply]

Wouldn't it be more logical to use |format=no instead of |noformat=yes? Obviously the default would be "yes" in this case. — Martin (MSGJ · talk) 16:50, 17 April 2011 (UTC)[reply]
I don't really have any strong preference; I'd not really thought about it either way. I've created a template {{ntsy}} that's waiting for this edit, but that's easy enough to change. — OwenBlacker (Talk) 21:41, 17 April 2011 (UTC)[reply]
 Done. Please let me know if this does not work correctly. — Martin (MSGJ · talk) 12:26, 18 April 2011 (UTC)[reply]
Perfect, thank you. — OwenBlacker (Talk) 12:34, 24 April 2011 (UTC)[reply]

Proposed fix for negative numbers[edit]

I going take more time to proof my fix. I ran across situations where #expr: choked on comma delimited numbers. I seems like this does not happen in the current version. So ignore the following for now. Currently the code in the sandbox works.

This template generates output that facilitates the sorting of numeric values in sortable tables. Currently the template fails to generate output that allows negative numbers to be sorted correctly. I have a version that fixes this in the template's sandbox and some test cases here. The sandbox version includes more conditionals and generates a longer hidden sort key. If you think it might break some pages then maybe the template should be forked. –droll [chat] 22:24, 23 August 2011 (UTC)[reply]

I'm happy to apply your code if you are sure it is working correctly. I don't see foresee any problems with this approach. — Martin (MSGJ · talk) 07:49, 29 August 2011 (UTC)[reply]
I'm confident that the code is good. My only concern is that it will use more resources and might break a page, but the page would have to have a lot of other stuff as well as tables. –droll [chat] 09:23, 29 August 2011 (UTC)[reply]
P.S. I included a quite parameter that can be used by the {{ntsh}} template. I'll clean up the documentation and propose that {{ntsh}} become a front end for this template. {{Convert}} transcludes {{ntsh}}. –droll [chat] 09:34, 29 August 2011 (UTC)[reply]
Okay, let me know when you are ready. Per WP:PERF I don't think you need to worry about performance issues. Do you mean quiet? And do you think we could expand the ntsh acronym by moving the template to a clearer name? — Martin (MSGJ · talk) 19:25, 29 August 2011 (UTC)[reply]
Your right the name of the parameter is quiet. I added {{editprotected}} template –droll [chat] 03:57, 30 August 2011 (UTC)[reply]
Made a few tweaks to whitespace and deployed. Let me know if anything goes wrong! — Martin (MSGJ · talk) 08:58, 31 August 2011 (UTC)[reply]

Thanks, MSGJ. –droll [chat] 10:23, 31 August 2011 (UTC)[reply]

I haven't dealt with this template in a while, but it seems like something else has changed. Before, I believe you could also put text in the argument and it would work fine. Now, something like &lt;span data-sort-value="1000000000000000000♠"&gt;10th&lt;/span&gt; produces the following:

10th

With the PRE tag and all. Weird? I'm also getting tons of errors in another template that uses this template, and in the process of debugging it, hopefully it has nothing to do with the most recent edit, although I suspect it probably does since the arguments over there also include some text at the end. Gary King (talk · scripts) 18:18, 10 October 2011 (UTC)[reply]
This page is a mess, at least on the browser I'm using right now (Firefox 3.6.6 Mac OS X). Something tells me that the new code supports numbers only, and no letters or symbols. I am going to use some ntsh until there is another solution in place. mechamind90 20:30, 12 October 2011 (UTC)[reply]

Proposed edit: explicitly state how to use the template for following text[edit]

I was confused a bit by this article. It mentioned that the template is useful if text follows a number, but didn't specifically state how the template was supposed to be used in that situation. I figured it out, you simply type {{nts | 123,456}} and put text after it to achieve that result. I would recommend that this be stated explicitly somehow. The wikitext "{{nts | 426,004}} as of January 2012" achieves an output of "426,004 as of January 2012" but with the numerical sort key 426,004. Please state this in the article, I can't edit it myself. I believe it will be highly useful to others, and should only serve to make the article better. Thanks. Jessemv (talk) 05:04, 13 January 2012 (UTC)[reply]

Hi Jessemv, the documentation for this template is held at Template:Number table sorting/doc and this is fully editable by anyone, so please make the changes you see fit. Regards — Martin (MSGJ · talk) 14:39, 13 January 2012 (UTC)[reply]
Brilliant, I'll try my best. :) Jessemv (talk) 22:53, 13 January 2012 (UTC)[reply]

And now for something completely different ...[edit]

I was looking at this template wondering whether we could do the same thing quicker; whether the code could be simplified (look, for example at {{number table sorting/positive}}). Sure it could thought I and we could easily extend the range a bit whilst we're at it.

We're limitted to a range of ±1016 with a precision of 6 decimal places. Look at the hidden output. There's an "&" and twenty-three digits (unless it's out of range). The first digit is a "1" or "0" (for positive & negative), I thought, we could get five times the range by going all the way up to "9". The next sixteen digits are calculated one by one. This is inefficient. The final six digits are calculated altogether. This is better; so why not calculate those sixteen digits like that? And why not? So I got to work on a new design.

To cut a medium-length story short, it's just dawned on me that we're doing it all wrong. I'm planning to significantly simplify the code and at the same time vastly expand the range. I'm not talking five times the range anymore, not five hundred time nor even five million; more like five thousand octoquadragintillion. It's simple. Don't do the same thing quicker do something better & quicker. Instead of having the first digit represent the sign have the first four digits represent the sign and the order of magnitude. The next fourteen digits will represent the significand.

Instead of using zeros to show magnitude, use numbers. We'll be looking at a range of about ±10300 to ±10–300 instead of the current ±1016 to ±10–6. JIMp talk·cont 08:00, 9 May 2012 (UTC)[reply]

Don't forget that {{Ntsh}} uses this template's sub-templates. –droll [chat] 18:47, 9 May 2012 (UTC)[reply]

No, actually that's what lead me here in the first place. Well, I've written the new version. I'm afraid I haven't quite simplified the code. But I haven't significantly increased it either. There's about the same ammount of code as the current version (so I'm not expecting it to break anything). But what it does achieve is the promised increase in range. JIMp talk·cont 01:52, 10 May 2012 (UTC)[reply]

Okay, four and a half months later I'm back to finish off the job. The new code has been tested and things look good. It going live. JIMp talk·cont 01:16, 27 September 2012 (UTC)[reply]

Addition of a suffix parameter[edit]

There is a prefix parameter with this template, but would it be very difficult to add a suffix parameter? If this parameter was to be added, it would be like this:

{{nts|10000|suffix=&nbsp;(est.)}} would display: 10,000 (est.)

Would this be difficult to implement? Thanks. Dustin (talk) 18:39, 20 June 2014 (UTC)[reply]

Protected edit request on 22 September 2014[edit]

Template:Nts redirects to Template:Number table sorting. Surely the redirect should be tagged {{R from template shortcut}} NickGibson3900 Talk 08:06, 22 September 2014 (UTC)[reply]

Done --Redrose64 (talk) 18:35, 22 September 2014 (UTC)[reply]

Sort key quirks[edit]

While checking Module:Val I have noticed many minor differences regarding sort keys. The issues probably do not affect table sort orders, but there is a problem in the template if anyone wants a challenge. I believe the val sort keys shown below (generated by the code at function make_table_or_sort in Module:Convert/sandbox) are correct.

  • 6989100000000000000♠1×10^−11{{nts|1e-11|debug=yes}}
  • 6989100000000000000♠1×10−11{{val/sandboxlua|1e-11|debug=yes}}
  • 7001333000000000000♠33.3{{nts|33.3|debug=yes}}
  • 7001333000000000000♠33.3{{val/sandboxlua|33.3|debug=yes}}
  • 7000137999999999999♠1.38{{nts|1.38|debug=yes}}
  • 7000137999999999999♠1.38{{val/sandboxlua|1.38|debug=yes}}
  • 6993770000000000000♠7.7×10^−7{{nts|7.7e-7|debug=yes}}
  • 6993769999999999999♠7.7×10−7{{val/sandboxlua|7.7e-7|debug=yes}}

Johnuniq (talk) 03:49, 28 July 2015 (UTC)[reply]

Didn't sort[edit]

I tried to use this template at List of household surveys in the United States, but it still doesn't sort correctly. Any ideas of how to solve this? Mikael Häggström (talk) 07:57, 2 November 2016 (UTC)[reply]

Do you mean the items in the "Number of people" column which start with "Over"? That's because the sort key occurs after "Over ", so the item is sorted as text starting with "O". One solution would be to use ntsh at the beginning to produce the key, followed with "Over 50,000" to produce the visible text. That requires duplicating the number. Another solution would be to use {{val}} like this:
{{val|50,000|fmt=commas|p=Over&#32;}}
Using &#32; generates a space, and p= makes "Over " a prefix that is displayed before the value, but after the hidden sort key. A difference is that val makes all the text "no wrap".
However, you cannot use val for all the items because it cannot handle "3.5 million" (it can only show numbers like 3.5×106). The sort keys produced by val and ntsh are almost always the same, so you could use val for some items and ntsh/nts for others. However, it might be simpler to use ntsh/nts for all entries and duplicate the values with "Over". Johnuniq (talk) 09:42, 2 November 2016 (UTC)[reply]
Thanks for identifying the problem! I switched Over___ to ___+. Mikael Häggström (talk) 08:08, 3 November 2016 (UTC)[reply]

Precision parameter request[edit]

Jimp et al, I have embedded an expression template into this template and I want the output to round to a precision of two decimal places. Right now, {{nts|{{#expr:614.204+373.6684+207.5538+1978.7031+3.5434}}}} produces 3,177.6727 when I want it to produce 3,177.67. Can anyone assist by either adding a precision parameter within this or the expression template? Cheers, Hwy43 (talk) 03:26, 7 December 2016 (UTC)[reply]

@Hwy43: The template {{round}} can be used. However, since you are using #expr, it supports rounding. The syntax is a shock, but it works.
  • {{nts|{{#expr:614.204+373.6684+207.5538+1978.7031+3.5434round2}}}}3,177.67
It would give ugly results if an error occurred in the calculation, and the sort key is based on the rounded value which is perhaps suboptimal. However, it should do what is needed. Johnuniq (talk) 04:02, 7 December 2016 (UTC)[reply]
Johnuniq, thanks. I've used the {{round}} template. Another question though... {{nts|{{round|{{#expr:1114.9921+1157.0342+897.7522+1054.056+11.1526}}|2}}}} renders 3,569.1, yet I want it to produce 3,569.10 (so that all entries within a column consistently have two decimal places). Is there a way to force a zero as the last decimal place? I'm having a similar problem with the {{percentage}} template. I want {{percentage|912100|921727|1}} to produce 99.0%, yet it renders 99%. Hwy43 (talk) 04:41, 7 December 2016 (UTC)[reply]
I think you would need some really ugly hacks to display trailing zeroes. Template syntax is not my strong point and you might ask at WP:WikiProject Templates. Hmm, I just found {{decimals}} which might help if used instead of round. Johnuniq (talk) 04:48, 7 December 2016 (UTC)[reply]
{{Decimals}} worked. See how {{nts|{{Decimals|{{#expr:877.5886+1248.4023+1443.1108}}|2}}}} produces 3,569.10. Haven't been able to get it or {{rnd}} to force the trailing zero within {{percentage}} yet. I'll ask at Template talk:Percentage and if no luck I'll approach WP:WikiProject Templates as suggested. Hwy43 (talk) 06:51, 7 December 2016 (UTC)[reply]
Discovered the pad parameter at {{percentage}}. {{percentage|912100|921727|1|pad=yes}} now produces 99.0%. We may want to consider implementing a pad parameter here as well to avoid the ugly hack workarounds described above. Hwy43 (talk) 07:00, 7 December 2016 (UTC)[reply]

Nts template in other templates[edit]

Can this be used in other templates? One user keeps adding it to Template:Player3. However, it breaks the table as - (hyphen), – (ndash) and — (mdash) can no longer be used there. – Sabbatino (talk) 22:41, 27 December 2016 (UTC)[reply]

Minor error in error message[edit]

TheDJ: The switch to the module has caused minor Linter errors when the variable %s is empty. The error message looks like this: Error in [[Template:Nts]]: '' is not a valid number. Note the two consecutive single quote marks, which the parser interprets as opening italic formatting. Can you please make a minor adjustment to insert a space between the two quote marks, or do something else to prevent this formatting error? Thanks. – Jonesey95 (talk) 10:39, 30 March 2019 (UTC)[reply]

Jonesey95, sure, ill fix it later today. —TheDJ (talkcontribs) 13:41, 30 March 2019 (UTC)[reply]
Don't think thats new though... if i remember the testcase results correctly, it did that before as well... —TheDJ (talkcontribs) 13:43, 30 March 2019 (UTC)[reply]
Jonesey95, ok, invalid input is now allowed —TheDJ (talkcontribs) 12:37, 31 March 2019 (UTC)[reply]
Thanks, that seems to have cleared up the problem at Template:WDL/doc, for example. – Jonesey95 (talk) 19:14, 31 March 2019 (UTC)[reply]

NTS template modification[edit]

TheDJ: the recent modification is causing problems at 2019 Alberta municipal censuses. Historically I have coded {{nts|}} in blank fields until the census results are published and it simply rendered as nothing. The old way was helpful because I didn't have to comment out the empty template previously (i.e. <!--{{nts|}}-->). Cheers, Hwy43 (talk) 21:38, 30 March 2019 (UTC)[reply]

Hwy43, invalid input is now allowed. —TheDJ (talkcontribs) 12:37, 31 March 2019 (UTC)[reply]
Thanks! Hwy43 (talk) 21:11, 1 April 2019 (UTC)[reply]

No scientific notation[edit]

List of legislatures by number of members looks rather silly with the populations of China and India in scientific notation. Is there a way to suppress this and use regular numbers? Or perhaps just converting everything to data-sort-type="number" would be better? Would have to figure out how to do that automatically instead of by hand... Reywas92Talk 05:22, 19 April 2019 (UTC)[reply]

It appears there is no way to disable that scientific notation display. If I ever had time I might fix that but meanwhile I put a kludge in the article to workaround the problem. Johnuniq (talk) 07:20, 19 April 2019 (UTC)[reply]
A fix for this would be great. List of mergers and acquisitions by Alphabet also looks silly with scientific notation. - Samuel Wiki (talk) 05:28, 7 June 2019 (UTC)[reply]

Requested move 31 May 2019[edit]

The following is a closed discussion of a requested move. Please do not modify it. Subsequent comments should be made in a new section on the talk page. Editors desiring to contest the closing decision should consider a move review after discussing it on the closer's talk page. No further edits should be made to this discussion.

The result of the move request was: moved. There is a consensus for this requested move. (closed by non-admin page mover) qedk (t c) 19:44, 9 June 2019 (UTC)[reply]


– No apparent reason for the module to have an abbreviated name when the template's name has the abbreviation expanded. * Pppery * it has begun... 23:31, 31 May 2019 (UTC)[reply]

  • Support - modules tend to follow the naming standards for templates, and the principle of "function should be clear from the template name" is a reasonable one. -- Netoholic @ 03:34, 1 June 2019 (UTC)[reply]
  • Support per above. Note though that {{Dts}} is at the abbreviated name, but I'd support its change as well. --Gonnym (talk) 08:36, 1 June 2019 (UTC)[reply]
    My mistake. In that case, I am going to withdraw Module:Dts from this nomination and open a new one for both the template and the module because I feel that conflating "Rename module to match template" and "Expand abbreviation in template name" is more confusing than helpful. * Pppery * it has begun... 12:26, 1 June 2019 (UTC)[reply]
    @Netoholic and Gonnym: Template talk:Dts#Requested move 1 June 2019. * Pppery * it has begun... 12:31, 1 June 2019 (UTC)[reply]
  • Before I can make this change, can you tell me which templates are affected by this (as well as Module:Dts), Pppery? Since modules can't have redirects, I cannot readily make the changes since templates will need to be edited as well. --qedk (t c) 19:33, 9 June 2019 (UTC)[reply]
    @QEDK: Yeah, module moving is a mess like that. The trick I use to determine which templates use a module is to do search for insource:"#invoke:<name>". In this case, that search suggests that {{Number table sorting}} and {{Ntsh}} are the templates that need to be updated. * Pppery * it has begun... 19:37, 9 June 2019 (UTC)[reply]
    Done now. I fixed a few in the documentation with bad regex, not sure if there's more. --qedk (t c) 19:44, 9 June 2019 (UTC)[reply]

The above discussion is preserved as an archive of a requested move. Please do not modify it. Subsequent comments should be made in a new section on this talk page or in a move review. No further edits should be made to this section.

Template-protected edit request on 8 December 2019[edit]

Please consider adding a text alignment option |align=, defaulted to the right (e.g.:align={{{align|right}}}). Guarapiranga (talk) 19:33, 8 December 2019 (UTC)[reply]

 Not done for now: please establish a consensus for this alteration before using the {{edit template-protected}} template. The same request at Template talk:Date table sorting#Template-protected edit request on 8 December 2019 was objected to. Cabayi (talk) 20:04, 8 December 2019 (UTC)[reply]
That's not an appropriate response, Cabayi.
  1. The page you're linking clearly states it is not one of Wikipedia's policies or guidelines.
  2. The {{edit template-protected}} template itself does not say anything about establishing a consensus before using its use. In fact, it says the very opposite:

    If you have noticed an error or have a suggestion for a simple, non-controversial change, you can submit an edit request

  3. The very WP:consensus page you're linking defines consensus as absence of objections. If there are no objections, there already is consensus. The appropriate response to this is to either say let's wait 48h and see if there are any objections, or simply waiting that time period before acting upon the request.
Guarapiranga (talk) 20:28, 8 December 2019 (UTC)[reply]
 Not done There's no reason to believe that Gonnym wouldn't oppose this request as well; alignment has just as little to do with sorting numbers as sorting dates. * Pppery * it has begun... 21:03, 8 December 2019 (UTC)[reply]
Who's Gonnym?? What reason is there to believe that he would? Are we supposed to read his mind? And how's that relevant?? What matters to WP:consensus is whether anyone expresses opposition (not whether someone would). Do you? Guarapiranga (talk) 21:08, 8 December 2019 (UTC)[reply]
Gonnym was the user who opposed Template talk:Date table sorting#Template-protected edit request on 8 December 2019, which was linked by Cabayi above. The rest of your comment consists of speculation that proved to be false, rendering it moot. (And before you ask, notifying users who participated in related discussions does not constitute inappropriate canvassing). * Pppery * it has begun... 21:47, 8 December 2019 (UTC)[reply]
Hey,thanks for the mention. As I've commented on the previous request, what does alignment have to do with sorting? Oppose this request. Gonnym (talk) 21:19, 8 December 2019 (UTC)[reply]
Are you opposing adding alignment as an option on this template simply based on its name, Gonnym? Sortable numbers on a table are almost always right justified. Why not save editors some trouble, and have it as an option (defaulted to the right)? {{n+p}} does, why shouldn't {{nts}}? (and {{dts}}!) Guarapiranga (talk) 13:46, 10 December 2019 (UTC)[reply]
You can't really use the fact that template "x" does something, when you were the one changed it to do it just recently. I mainly oppose your proposal because it mixes two completely different functions. One is a simple behind the scene code that handles the sorting of a cel in a column. The other adds style to the cell. This could potentially lead to a lot of issues, major or small. Have you checked all transclusions to see how this template is used? Are you sure that this won't cause any issue? Also, you asked for it to be default right-aligned. Again, can you share your research on how many pages have set it to right-aligned compared to any other alignment? --Gonnym (talk) 14:07, 10 December 2019 (UTC)[reply]
@Gonnym: You can't really use the fact that template "x" does something, when you were the one changed it to do it just recently.
It wasn't; Frietjes built the option in from day 1 (check the history before pointing fingers, perhaps). Guarapiranga (talk) 14:15, 10 December 2019 (UTC)[reply]
My mistake, but unless you actually comment on something which is relevant and not nitpick a mistake I made, that doesn't change anything. --Gonnym (talk) 14:22, 10 December 2019 (UTC)[reply]
Gonnym has given excellent answers which indicate that the edit request should not proceed. Johnuniq (talk) 22:46, 10 December 2019 (UTC)[reply]

how to put international number format or gaps into number?[edit]

tried to use format=gaps parameter to use gaps in the template, but it did not like it. when using international thousands separators like , or ٬ or arabic thousands separator ٬ the sorting does not work correctly. how would one do this? ThurnerRupert (talk) 01:10, 15 August 2023 (UTC)[reply]

ntsh[edit]

The docs for Template:Number table sorting hidden have a deprecation notice directing editors to use this template instead, but I don't see anything in the docs that make clear how one would achieve the same result. It looks to me like data-sort-value might be the better substitute? Walkersam (talk) 00:23, 20 April 2024 (UTC)[reply]

I don't have an answer to your point about the deprecation notice, but both templates use data-sort-value. The following shows input to Special:ExpandTemplates and the result:
{{nts|123456789.00123}}
{{ntsh|123456789.00123}}

<span data-sort-value="7008123456789001230♠">123,456,789.00123</span>
<span data-sort-value="7008123456789001230♠" style="display:none"></span>
Johnuniq (talk) 01:01, 20 April 2024 (UTC)[reply]