From 992c4b0bf757cd79e0611fcffa75a6cc83accf79 Mon Sep 17 00:00:00 2001 From: Lisa-Ann B Date: Wed, 18 Oct 2023 11:25:02 -0400 Subject: [PATCH] Sort group select dropdown, add sortOnProperty function - #981 --- src/components/custom/edit/GroupSelect.js | 7 ++++++- src/components/custom/js/functions.js | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/components/custom/edit/GroupSelect.js b/src/components/custom/edit/GroupSelect.js index 77680dd92..1639324ed 100644 --- a/src/components/custom/edit/GroupSelect.js +++ b/src/components/custom/edit/GroupSelect.js @@ -1,4 +1,4 @@ -import React, {useContext} from 'react'; +import React, {useContext, useEffect} from 'react'; import {QuestionCircleFill} from "react-bootstrap-icons"; import {Form} from 'react-bootstrap'; import SenNetPopover from "../../SenNetPopover"; @@ -6,6 +6,11 @@ import AppContext from "../../../context/AppContext"; const GroupSelect = ({groups, onGroupSelectChange, entity_type, plural}) => { const {cache} = useContext(AppContext) + + useEffect(() => { + groups.sortOnProperty('displayname') + }) + return ( <> diff --git a/src/components/custom/js/functions.js b/src/components/custom/js/functions.js index 30fb33ddd..495086da3 100644 --- a/src/components/custom/js/functions.js +++ b/src/components/custom/js/functions.js @@ -377,6 +377,24 @@ Object.assign(String.prototype, { } }) +Object.assign(Array.prototype, { + sortOnProperty(key) { + return this.sort((a, b) => { + let fa = a[key].toLowerCase(), + fb = b[key].toLowerCase() + + if (fa < fb) { + return -1 + } + if (fa > fb) { + return 1 + } + return 0 + }) + } +}) + + export const flipObj = (obj) => { return Object.keys(obj).reduce((ret, key) => { ret[obj[key]] = key;