To use this function add it to the class library file named Utilities.ts
GitHub Repository: https://github.com/SimonSparksUK/Orchestrator
Filename: Utilities.ts
Function sortArrayOfStringLocale
Description: Orchestrator Function to Sort an Array of Strings using International Locale
publicstaticsortArrayOfStringLocale(arr:string[],enumSortOrder:SortOrder=SortOrder.Forward):string[]{
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator/Collator#casefirst
letobjCollatorOptions: Intl.CollatorOptions={
usage:"sort",
localeMatcher:"lookup",
numeric:true,
caseFirst:"false",
sensitivity:"base",
ignorePunctuation:true,
//collation: "big5han" | "compat" | "dict" | "direct" | "ducet" | "emoji" | "eor" | "gb2312" | "phonebk" | "phonetic" | "pinyin" | "reformed" | "searchjl" | "stroke" | "trad" | "unihan" | "zhuyin"
};
letobjIntlCollator: Intl.Collator=newIntl.Collator("en",objCollatorOptions);
arr=arr.sort((strA:string,strB:string):number=>{
if (enumSortOrder===SortOrder.Forward){
returnobjIntlCollator.compare(strA,strB)
}
elseif (enumSortOrder===SortOrder.Reverse){
returnobjIntlCollator.compare(strB,strA)
}
});
returnarr;
}Enumeration: SortOrder
exportenumSortOrder{
Forward="Forward",
Reverse="Reverse"
}Discover more from Cloud Build Tools
Subscribe to get the latest posts sent to your email.
