Function: Sort an Array of Strings using International Locale

by Simon Sparks · 29 January 2026

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

public static sortArrayOfStringLocale(arr: string[], enumSortOrder: SortOrder = SortOrder.Forward): string[] {

  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator/Collator#casefirst

  let objCollatorOptions: 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"
  };

  let objIntlCollator: Intl.Collator = new Intl.Collator("en", objCollatorOptions);

  arr = arr.sort((strA: string, strB: string): number => {
    if (enumSortOrder === SortOrder.Forward) {
      return objIntlCollator.compare(strA, strB)
    }
    else if (enumSortOrder === SortOrder.Reverse) {
      return objIntlCollator.compare(strB, strA)
    }
  });

  return arr;
}

Enumeration: SortOrder

export enum SortOrder {
  Forward = "Forward",
  Reverse = "Reverse"
}

Discover more from Cloud Build Tools

Subscribe to get the latest posts sent to your email.

You may also like