-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
DataTable: Wrong alphabetical sorting for Norwegian (and others?) #4623
Comments
Hi @rubjo, Interesting! @FlipWarthog updated all sorting functions using Intl.Collator method @FlipWarthog could you please check this issue? |
@mertsincan Yep, you can assign this to me and I'll do some analysis. |
This should all work because its using static localeComparator(locale) {
return new Intl.Collator(locale, { numeric: true }).compare;
} It will use your currently set Locale for how to sort based on your language. |
Thanks to @FlipWarthog for looking into this. Meanwhile, I did a quick test on my Mac with Safari, Chrome, Firefox and Edge - which all report In which case I guess But: For some reason, the browser I'm currently using (Arc), is the outlier here and reporting In any case, @FlipWarthog's change makes it possible for the app to be in charge of this, instead of leaving it to the browser. Update: Arc just needed to have Norwegian added to the top of the list of languages in Settings, doh. I guess the other browsers automatically inferred this on first launch because of the OS locale. |
Thanks @FlipWarthog, I'll merge your PR in 4.0. I think we can use locale option instead of localeCode. |
@mertsincan No problem, let me know if you need me to rebase or make any updates, or if you just want to take over the PR. 👍 |
I have the same issue even with default en-US locale: <template>
<DataTable :value="items">
<Column field="name" sortable header="name"/>
<Column field="value" sortable header="value"/>
</DataTable>
</template>
<script setup lang="ts">
import DataTable from 'primevue/datatable';
import Column from 'primevue/column';
import {ref} from "vue";
let items = ref(
[
{name: "@2833", value: "2833"},
{name: "@a0", value: "a0"},
{name: "@09091a", value: "09091a"},
{name: "@11-", value: "11-"},
{name: "@168a", value: "168a"},
]);
</script> Here is the sorted table, which is not sorted correctly: |
One more question: why here is a simple demo:
Seems we should use |
@goldenbull its because this comparer is ONLY used when the values are strings see: So when comparing strings
|
@melloware I'm confused 😕 Well, my experience is mainly limited in C/C++/C#/Java, the "strong type" world. When a coder want to show a group of data in grid, it's the coder's duty to provide the data in correct datatype, string or numeric. The grid should sort the data according to it's datatype: if numeric, sort them as numeric; if string, sort them alphabetically. Think about the above example, I have a string array:
what is the correct (or expected) sorted result should it be? seems that primevue provide an intelligent(automatic) way to deal with numeric data in string datatype, well, that's good, but on the other hand, the grid lost the ability to sort string data alphabetically. Maybe here should be some options for column? Something like
|
@goldenbull Correct.... but it should only do it if the column is a string. sort(value1, value2, order = 1, comparator, nullSortOrder = 1) {
const result = this.compare(value1, value2, comparator, order);
let finalSortOrder = order;
// nullSortOrder == 1 means Excel like sort nulls at bottom
if (this.isEmpty(value1) || this.isEmpty(value2)) {
finalSortOrder = nullSortOrder === 1 ? order : nullSortOrder;
}
return finalSortOrder * result;
},
compare(value1, value2, comparator, order = 1) {
let result = -1;
const emptyValue1 = this.isEmpty(value1);
const emptyValue2 = this.isEmpty(value2);
if (emptyValue1 && emptyValue2) result = 0;
else if (emptyValue1) result = order;
else if (emptyValue2) result = -order;
else if (typeof value1 === 'string' && typeof value2 === 'string') result = comparator(value1, value2);
else result = value1 < value2 ? -1 : value1 > value2 ? 1 : 0;
return result;
}, It only uses the comparator if |
notice my above example, here are two strings: |
Counterpoint: true is correct order and false is incorrect order. Original PrimeReact Issue: primefaces/primereact#4903 |
So how to sort |
Agreed I wonder if there is a better way to do this or maybe Or maybe something set in the main PrimeVue config? |
how about this solution:
But this solution needs more code refactor and is error-prone when sorting multiple columns. Simpler solution is an extra option in main PrimeVue config and set |
Describe the bug
DataTable doesn't sort correctly with Nordic characters: "Å" is last in Norwegian, while DataTable sorts it immediately after "A".
localeCompare and Intl.Collator sort these correctly.
I suspect the same is the case for Danish, Swedish and possibly others.
How can I sort correctly by default – or how can I find documentation on how to do custom sorting?
Reproducer
https://stackblitz.com/edit/mczqps?file=src%2FApp.vue
PrimeVue version
3.36.0
Vue version
3.x
Language
TypeScript
Build / Runtime
Vite
Browser(s)
No response
Steps to reproduce the behavior
No response
Expected behavior
No response
The text was updated successfully, but these errors were encountered: