Find lowest sorting order

    0

    1

    Giovanny Gongora

    Codiga's TypeScript Recipes

    Finds the lowest index at which a value should be inserted into an array in order to maintain its sorting order.

    const sortedIndex = (arr: Array<number>, n: number) => {
      const isDescending: boolean = arr[0] > arr[arr.length - 1];
      const index: number = arr.findIndex(el => (isDescending ? n >= el : n <= el));
      return index === -1 ? arr.length : index;
    };
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Explore
    • Cookbooks
    • Playground
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.