From f9360b8ade9acc11b7cd45a8958fc90a08342727 Mon Sep 17 00:00:00 2001 From: FlipWarthog Date: Fri, 6 Oct 2023 18:59:00 -0400 Subject: [PATCH] Add new sort everywhere needed, update API docs --- components/lib/carousel/Carousel.vue | 11 ++------ components/lib/datatable/DataTable.d.ts | 5 ++++ components/lib/dataview/DataView.vue | 9 +------ .../lib/galleria/GalleriaThumbnails.vue | 11 ++------ components/lib/treetable/TreeTable.vue | 26 ++++--------------- doc/common/apidoc/index.json | 10 ++++++- 6 files changed, 24 insertions(+), 48 deletions(-) diff --git a/components/lib/carousel/Carousel.vue b/components/lib/carousel/Carousel.vue index f0064fe85..cd189b170 100755 --- a/components/lib/carousel/Carousel.vue +++ b/components/lib/carousel/Carousel.vue @@ -101,7 +101,7 @@ import ChevronLeftIcon from 'primevue/icons/chevronleft'; import ChevronRightIcon from 'primevue/icons/chevronright'; import ChevronUpIcon from 'primevue/icons/chevronup'; import Ripple from 'primevue/ripple'; -import { DomHandler, UniqueComponentId } from 'primevue/utils'; +import { DomHandler, UniqueComponentId, ObjectUtils } from 'primevue/utils'; import BaseCarousel from './BaseCarousel.vue'; export default { @@ -553,15 +553,8 @@ export default { _responsiveOptions.sort((data1, data2) => { const value1 = data1.breakpoint; const value2 = data2.breakpoint; - let result = null; - if (value1 == null && value2 != null) result = -1; - else if (value1 != null && value2 == null) result = 1; - else if (value1 == null && value2 == null) result = 0; - else if (typeof value1 === 'string' && typeof value2 === 'string') result = comparer(value1, value2); - else result = value1 < value2 ? -1 : value1 > value2 ? 1 : 0; - - return -1 * result; + return ObjectUtils.sort(value1, value2, -1, comparer); }); for (let i = 0; i < _responsiveOptions.length; i++) { diff --git a/components/lib/datatable/DataTable.d.ts b/components/lib/datatable/DataTable.d.ts index 61275bdb5..84d063cc2 100755 --- a/components/lib/datatable/DataTable.d.ts +++ b/components/lib/datatable/DataTable.d.ts @@ -887,6 +887,11 @@ export interface DataTableProps { * Order to sort the data by default. */ sortOrder?: number | undefined; + /** + * Determines how null values are sorted. + * @defaultValue 1 + */ + nullSortOrder?: number; /** * Default sort order of an unsorted column. * @defaultValue 1 diff --git a/components/lib/dataview/DataView.vue b/components/lib/dataview/DataView.vue index 95ca01936..b1e14fc3e 100755 --- a/components/lib/dataview/DataView.vue +++ b/components/lib/dataview/DataView.vue @@ -115,15 +115,8 @@ export default { value.sort((data1, data2) => { let value1 = ObjectUtils.resolveFieldData(data1, this.sortField); let value2 = ObjectUtils.resolveFieldData(data2, this.sortField); - let result = null; - if (value1 == null && value2 != null) result = -1; - else if (value1 != null && value2 == null) result = 1; - else if (value1 == null && value2 == null) result = 0; - else if (typeof value1 === 'string' && typeof value2 === 'string') result = comparer(value1, value2); - else result = value1 < value2 ? -1 : value1 > value2 ? 1 : 0; - - return this.sortOrder * result; + return ObjectUtils.sort(value1, value2, this.sortOrder, comparer); }); return value; diff --git a/components/lib/galleria/GalleriaThumbnails.vue b/components/lib/galleria/GalleriaThumbnails.vue index f916eb6bc..5df896fad 100755 --- a/components/lib/galleria/GalleriaThumbnails.vue +++ b/components/lib/galleria/GalleriaThumbnails.vue @@ -68,7 +68,7 @@ import ChevronLeftIcon from 'primevue/icons/chevronleft'; import ChevronRightIcon from 'primevue/icons/chevronright'; import ChevronUpIcon from 'primevue/icons/chevronup'; import Ripple from 'primevue/ripple'; -import { DomHandler } from 'primevue/utils'; +import { DomHandler, ObjectUtils } from 'primevue/utils'; export default { name: 'GalleriaThumbnails', @@ -443,15 +443,8 @@ export default { this.sortedResponsiveOptions.sort((data1, data2) => { const value1 = data1.breakpoint; const value2 = data2.breakpoint; - let result = null; - if (value1 == null && value2 != null) result = -1; - else if (value1 != null && value2 == null) result = 1; - else if (value1 == null && value2 == null) result = 0; - else if (typeof value1 === 'string' && typeof value2 === 'string') result = comparer(value1, value2); - else result = value1 < value2 ? -1 : value1 > value2 ? 1 : 0; - - return -1 * result; + return ObjectUtils.sort(value1, value2, -1, comparer); }); for (let i = 0; i < this.sortedResponsiveOptions.length; i++) { diff --git a/components/lib/treetable/TreeTable.vue b/components/lib/treetable/TreeTable.vue index d4635314d..28f03c5c7 100755 --- a/components/lib/treetable/TreeTable.vue +++ b/components/lib/treetable/TreeTable.vue @@ -434,15 +434,8 @@ export default { _nodes.sort((node1, node2) => { const value1 = ObjectUtils.resolveFieldData(node1.data, this.d_sortField); const value2 = ObjectUtils.resolveFieldData(node2.data, this.d_sortField); - let result = null; - if (value1 == null && value2 != null) result = -1; - else if (value1 != null && value2 == null) result = 1; - else if (value1 == null && value2 == null) result = 0; - else if (typeof value1 === 'string' && typeof value2 === 'string') result = comparer(value1, value2); - else result = value1 < value2 ? -1 : value1 > value2 ? 1 : 0; - - return this.d_sortOrder * result; + return ObjectUtils.sort(value1, value2, this.d_sortOrder, comparer); }); return _nodes; @@ -462,22 +455,13 @@ export default { multisortField(node1, node2, index) { const value1 = ObjectUtils.resolveFieldData(node1.data, this.d_multiSortMeta[index].field); const value2 = ObjectUtils.resolveFieldData(node2.data, this.d_multiSortMeta[index].field); - let result = null; + const comparer = ObjectUtils.localeComparator(); - if (value1 == null && value2 != null) result = -1; - else if (value1 != null && value2 == null) result = 1; - else if (value1 == null && value2 == null) result = 0; - else { - if (value1 === value2) { - return this.d_multiSortMeta.length - 1 > index ? this.multisortField(node1, node2, index + 1) : 0; - } else { - if ((typeof value1 === 'string' || value1 instanceof String) && (typeof value2 === 'string' || value2 instanceof String)) - return this.d_multiSortMeta[index].order * ObjectUtils.localeComparator().compare(value1, value2); - else result = value1 < value2 ? -1 : 1; - } + if (value1 === value2) { + return this.d_multiSortMeta.length - 1 > index ? this.multisortField(node1, node2, index + 1) : 0; } - return this.d_multiSortMeta[index].order * result; + return ObjectUtils.sort(value1, value2, this.d_multiSortMeta[index].order, comparer); }, filter(value) { let filteredNodes = []; diff --git a/doc/common/apidoc/index.json b/doc/common/apidoc/index.json index e41b00b20..eb551eec0 100644 --- a/doc/common/apidoc/index.json +++ b/doc/common/apidoc/index.json @@ -18854,6 +18854,14 @@ "default": "", "description": "Order to sort the data by default." }, + { + "name": "nullSortOrder", + "optional": true, + "readonly": false, + "type": "number", + "default": "1", + "description": "Determines how null values are sorted." + }, { "name": "defaultSortOrder", "optional": true, @@ -55561,4 +55569,4 @@ } } } -} +} \ No newline at end of file