vault/ui/app/utils/sort-objects.js
claire bontempo c265f5a132
UI: helper sort-objects to alphabetize list items (#24103)
* move list to component

* use helper instead

* add changelog

* clarify changelog copy

* delete components now that helper is in use

* move helper to util, remove template helper invokation

* add optional sorting to lazyPaginatedQuery based on sortBy query attribute

* Add serialization to entity-alias and entity so that they can be sorted by name on list view

* Same logic as base normalizeItems for extractLazyPaginatedData so that metadata shows on list

* Add headers

---------

Co-authored-by: Chelsea Shaw <cshaw@hashicorp.com>
Co-authored-by: Chelsea Shaw <82459713+hashishaw@users.noreply.github.com>
2023-11-14 21:57:29 +00:00

20 lines
540 B
JavaScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
export default function sortObjects(array, key) {
if (Array.isArray(array) && array?.every((e) => e[key] && typeof e[key] === 'string')) {
return array.sort((a, b) => {
// ignore upper vs lowercase
const valueA = a[key].toUpperCase();
const valueB = b[key].toUpperCase();
if (valueA < valueB) return -1;
if (valueA > valueB) return 1;
return 0;
});
}
// if not sortable, return original array
return array;
}