Sorting Money / Integer with single, double and triple digits
I'm submitting a ... (check one with "x")
[ x ] bug report => Search github for a similar issue or PR before submitting
**Plunkr Case (Bug Reports)**
https://plnkr.co/edit/l4JYVcfiyrl3qB88Ym0v?p=preview
**Current behavior**
when clicking a column to sort and that column contains single, double and triple digits it sort incorrectly
**Expected behavior**
when sorting a column with an integer or money it should ascend and or descend correctly.
**Minimal reproduction of the problem with instructions**
https://plnkr.co/edit/l4JYVcfiyrl3qB88Ym0v?p=preview
(Integer Bug Sorting)
1.) Click on quantity column
2.) Column sorts incorrectly
(Decimal Bug Sorting)
1.) Click on price column
2.) Column sorts incorrectly
**What is the motivation / use case for changing the behavior?**
Sort with accuracy
**Please tell us about your environment:**
* **Angular version:** 4.4.4
* **PrimeNG version:** 4.3.0
<!-- Check whether this is still an issue in the most recent Angular version -->
* **Browser:** [all | Chrome XX | Firefox XX | IE XX | Safari XX | Mobile Chrome XX | Android X.X Web Browser | iOS XX Safari | iOS XX UIWebView | iOS XX WKWebView ]
<!-- All browsers where this could be reproduced -->
All
In my opinion, the sorting behavior on integers with currency is more use case specific. You can use sortFunction of column component to make customized sorting.
The example provided above is a bug on sorting integers. While it is a bug on sorting integers its also a bug on sorting decimals / money. The expected behavior on an data table with a column sort is to provide sorting on a numerical column in numerical order. Using sortFunction to make a customized sorting for an numerical value is not the answer, its impractical.
The issue description is not properly formatted. Can you provide the plunker example(which as integers with currency) as well.
https://plnkr.co/edit/l4JYVcfiyrl3qB88Ym0v?p=preview
Sudheerj, The plnkr result had both examples one with integers and one with money / currency.
Thanks for your comments and understanding. My explanation with above all comments is here.
-
The issue template format is not proper and difficult to read. For example, the proper format of issue should be like this https://github.com/primefaces/primeng/issues/4394
-
The numbers with currency column will give the meaning that numbers having different currency values($,euro, rupee, pound). For example, imagine salary with different currencies of a company, https://datatables.net/ . In that situation, Unless it supports by default, I prefer sortFunction due to currency symbols. In your case, the columns are just numbers with string type.
-
Change your quantity and price columns as number type instead String type. Sorting on string type will sort for each digit(for example, if you sort them as strings, 3 is greater than 29 because 3 > 2).
this.cars = [
{"quantity":1,"item":"toaster","price":100.01},
{"quantity":2,"item":"fan","price":200.00},
{"quantity":3,"item":"ball","price":5.00},
{"quantity":5,"item":"coat","price":23.05},
{"quantity":21,"item":"drill","price":13.95},
{"quantity":221,"item":"computer","price":53.25},
{"quantity":1,"item":"shirt","price":3.00},
];
Hope I answer your all your questions.
Thanks for your comments and understanding.
I have set the code to the proper format you requested. https://plnkr.co/edit/l4JYVcfiyrl3qB88Ym0v?p=preview
Sudheerj, The example you given me with datatables.net sort feature works out the box on all column examples, quantity, date, currency, name etc. It would be great if primeng datatable could do the same. Nevertheless you mentioned if i set the column type as a number it would resolved the issue. Please advise how to set the column type so I can try your suggestion and see the results for the quantity column. I think if I'm able to set the column type in advance sorting could be done very easily then. I looked through primeng documentation and I'm unable to see such documentation on the column type. If you can provide a working example it would be appreciated and I would be willing to close the issue.
Please find my replies for your questions in detail
I have set the code to the proper format you requested. https://plnkr.co/edit/l4JYVcfiyrl3qB88Ym0v?p=preview
I asked you to format the issue(not the code) you created.
Sudheerj, The example you given me with datatables.net sort feature works out the box on all > > column examples, quantity, date, currency, name etc.
The specified sorting feature is working with number type only. That's why it is working nicely.
I think if I'm able to set the column type in advance sorting could be done very easily then.
It is basic sorting only, not advance one.
I looked through primeng documentation and I'm unable to see such documentation on the column type. If you can provide a working example it would be appreciated and I would be willing to close the issue.
1.The typescript interface fields defined with type as here https://www.typescriptlang.org/docs/handbook/interfaces.html. 2.Converting string to number https://stackoverflow.com/questions/34014342/convert-json-string-value-to-number The example is available for dataTable sorting if you can clone showcase code. https://github.com/primefaces/primeng/tree/master/src/app/showcase
In summary, this is not a PrimeNG specific issue.There won't be any type defined on component side. You need to change the type on your backend data.
hi Guys, In Primeng data- table support sorting as we assign the data in the columan. like the given example of @sudheerj :
this.cars = [
{"quantity":1,"item":"toaster","price":100.01},
{"quantity":2,"item":"fan","price":200.00},
{"quantity":3,"item":"ball","price":5.00},
{"quantity":5,"item":"coat","price":23.05},
{"quantity":21,"item":"drill","price":13.95},
{"quantity":221,"item":"computer","price":53.25},
{"quantity":1,"item":"shirt","price":3.00},
];
in this if we assign col value "price":3.00 then it's float value if we assign value like "price":"3.00" then primeng consider this value as String and sorting accourdingly.