你对此有什么看法?排序脚本检查对象是否为文件夹,将其放在顶部,然后处理实际排序(文件夹与文件夹和文件与文件)。
var files = [
  {
    name    : 'folder2/',
    filesize: null,
    filetype: 'folder'
  },
  {
    name    : 'folder1/',
    filesize: null,
    filetype: 'folder'
  },
  {
    name    : 'def.jpg',
    filesize: '1.2',
    filetype: 'jpg'
  },
  {
    name    : 'abc.pdf',
    filesize: '3.2',
    filetype: 'pdf'
  },
  {
    name    : 'ghi.doc',
    filesize: '1.5',
    filetype: 'doc'
  },
  {
    name    : 'jkl.doc',
    filesize: '1.1',
    filetype: 'doc'
  },
  {
    name    : 'pqr.pdf',
    filesize: '3.5',
    filetype: 'pdf'
  },
  {
    name    : 'mno.pdf',
    filesize: '3.5',
    filetype: 'pdf'
  }
];
/**
 * Sort an array of files and always put the folders at the top.
 * @access public
 * @param  {Array} array to sort
 * @param  {String} column to sort
 * @param  {Bool} asc
 * @return void
 */
function sortBy(array, column, asc) {
  if (asc == null) {
    asc = true;
  }
  array.sort(function(a, b) {
    // Put at the top the folders.
    if (a.filetype == 'folder' && b.filetype != 'folder') {
      return false;
    // Sort between folders.
    // The folders don't have a filesize and the type is always the same.
    // Process as a sort by name.
    // It doesn't need to respect the sens of sorting.
    } else if ((column == 'filesize' || column == 'filetype') && a.filetype == 'folder' && b.filetype == 'folder') {
      return a.name > b.name;
    // Normal sort.
    // The folders will be sorted together and the files togethers.
    } else {
      return asc ? a[column] > b[column] : a[column] < b[column];
    }
  });
}
sortBy(files, 'name', false);
console.log('by name', files);
sortBy(files, 'filesize', true);
console.log('by size', files);
sortBy(files, 'filetype', false);
console.log('by type', files);