<commit_before>// Libraries
import isNumber from 'lodash/isNumber';

import { TableData } from '@grafana/ui';

export function sortTableData(data: TableData, sortIndex?: number, reverse = false): TableData {
  if (isNumber(sortIndex)) {
    const copy = {
      ...data,
      rows: data.rows.map((row, index) => {
        return row;
      }),
    };

    copy.rows.sort((a, b) => {
      a = a[sortIndex];
      b = b[sortIndex];
      // Sort null or undefined separately from comparable values
      return +(a == null) - +(b == null) || +(a > b) || -(a < b);
    });

    if (reverse) {
      copy.rows.reverse();
    }<commit_msg>better sort function<commit_after>// Libraries
import isNumber from 'lodash/isNumber';

import { TableData } from '@grafana/ui';

export function sortTableData(data: TableData, sortIndex?: number, reverse = false): TableData {
  if (isNumber(sortIndex)) {
    const copy = {
      ...data,
      rows: [...data.rows].sort((a, b) => {
        a = a[sortIndex];
        b = b[sortIndex];
        // Sort null or undefined separately from comparable values
        return +(a == null) - +(b == null) || +(a > b) || -(a < b);
      }),
    };

    if (reverse) {
      copy.rows.reverse();
    }
{'fraction_non_alphanumeric': 0.16951915240423798, 'fraction_numerical': 0.0, 'mean_word_length': 2.468926553672316, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3097047', 'n_tokens_mistral': 400, 'n_tokens_neox': 391, 'n_words': 108}