// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
/** Order option for {@linkcode SortByOptions}. */ export function sortBy(array, selector, options) {
  const len = array.length;
  const indexes = new Array(len);
  const selectors = new Array(len);
  const order = options?.order ?? "asc";
  array.forEach((item, idx)=>{
    indexes[idx] = idx;
    const s = selector(item);
    selectors[idx] = Number.isNaN(s) ? null : s;
  });
  indexes.sort((ai, bi)=>{
    let a = selectors[ai];
    let b = selectors[bi];
    if (order === "desc") {
      [a, b] = [
        b,
        a
      ];
    }
    if (a === null) return 1;
    if (b === null) return -1;
    return a > b ? 1 : a < b ? -1 : 0;
  });
  for(let i = 0; i < len; i++){
    indexes[i] = array[indexes[i]];
  }
  return indexes;
}
//# sourceMappingURL=data:application/json;base64,[encoded data, 7084 characters]