Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Controling the sorting direction (asc/desc) #250

Open
Dev-iL opened this issue Feb 14, 2024 · 0 comments
Open

Controling the sorting direction (asc/desc) #250

Dev-iL opened this issue Feb 14, 2024 · 0 comments

Comments

@Dev-iL
Copy link

Dev-iL commented Feb 14, 2024

I'm looking for a way to specify the sort direction when sorting by name/modified. Can we do something like this?

import { compareAsc, compareDesc } from 'date-fns';

function lastModifiedSort(allFiles, direction = 'asc') {
  const folders = [];
  let files = [];
  for (let fileIndex = 0; fileIndex < allFiles.length; fileIndex++) {
    const file = allFiles[fileIndex];
    const keyFolders = (file.newKey || file.key).split('/');
    if (file.children) {
      folders.push(file);
    } else {
      file.name = keyFolders[keyFolders.length - 1];
      files.push(file);
    }
  }

  // Sort files based on direction
  if (direction === 'asc') {
    files = files.sort(compareAsc);
  } else {
    files = files.sort(compareDesc);
  }

  for (let folderIndex = 0; folderIndex < folders.length; folderIndex++) {
    const folder = folders[folderIndex];
    folder.children = lastModifiedSort(folder.children, direction);
  }

  let sortedFiles = [];
  sortedFiles = sortedFiles.concat(folders);
  sortedFiles = sortedFiles.concat(files);
  return sortedFiles;
}

export default function(files, direction) {
  return lastModifiedSort(files, direction);
}

Typescript equivalent:

import { compareAsc, compareDesc } from 'date-fns';

// Define an enum for sorting direction
enum SortDirection {
  ASCENDING = 'asc',
  DESCENDING = 'desc'
}

function sortByLastModified(payload, direction: SortDirection = SortDirection.ASCENDING) {
  const folders = [];
  let files = [];
  for (let fileIndex = 0; fileIndex < payload.length; fileIndex++) {
    const file = payload[fileIndex];
    const keyFolders = (file.newKey || file.key).split('/');
    if (file.children) {
      folders.push(file);
    } else {
      file.name = keyFolders[keyFolders.length - 1];
      files.push(file);
    }
  }

  // Sort files based on direction
  if (direction === SortDirection.ASCENDING) {
    files = files.sort(compareAsc);
  } else {
    files = files.sort(compareDesc);
  }

  for (let folderIndex = 0; folderIndex < folders.length; folderIndex++) {
    const folder = folders[folderIndex];
    folder.children = sortByLastModified(folder.children, direction);
  }

  let sortedFiles = [];
  sortedFiles = sortedFiles.concat(folders);
  sortedFiles = sortedFiles.concat(files);
  return sortedFiles;
}

export { sortByLastModified, SortDirection };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant