Skip to content

Commit

Permalink
Merge pull request #1280 from akto-api-security/fix/sorting_on_name
Browse files Browse the repository at this point in the history
fix sorting on displayname in inventory
  • Loading branch information
avneesh-akto authored Jul 15, 2024
2 parents eb7647c + 8cd5aff commit 7a3e713
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions apps/dashboard/web/polaris_web/web/src/util/func.js
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,27 @@ prettifyEpoch(epoch) {
},

sortFunc: (data, sortKey, sortOrder) => {
if(sortKey === 'displayName'){
let finalArr = data.sort((a, b) => {
const nameA = a?.displayName.toLowerCase() || a?.name.toLowerCase();
const nameB = b?.displayName.toLowerCase() || b?.name.toLowerCase();

// Define a regex to check if the name starts with a digit
const startsWithDigitA = /^\d/.test(nameA);
const startsWithDigitB = /^\d/.test(nameB);

// Alphabetical names should come first
if (startsWithDigitA && !startsWithDigitB) return 1;
if (!startsWithDigitA && startsWithDigitB) return -1;

// If both names either start with a digit or both don't, compare them directly
return nameA.localeCompare(nameB);
});
if(sortOrder > 0){
finalArr.reverse()
}
return finalArr
}
return data.sort((a, b) => {
if(typeof a[sortKey] ==='number')
return (sortOrder) * (a[sortKey] - b[sortKey]);
Expand Down

0 comments on commit 7a3e713

Please sign in to comment.