/*------------------------------------------------------------------------------
 src/lib/dj/djSort_.h
 
 Copyright (c) 2004-2006 Darby Johnston.
 All rights reserved.
 Use is subject to license terms.
------------------------------------------------------------------------------*/

namespace dj {

template <class T>
inline const List<T> qsort(const List<T> & in, QSortCompare * compare)
{
  const int size = in.size();
  if (! size) return List<T>();

  // Copy to temporary list.
  //
  const T ** tmp = new const T * [size];
  int i = 0;
  for (ConstListIt<T> it = in; it.isValid(); ++it, ++i)
    tmp[i] = &it();

  // Sort temporary list.
  //
  qsort(tmp, size, sizeof(T *), compare);

  // Copy from temporary list.
  //
  List<T> out;
  for (i = 0; i < size; ++i)
    out.add(*tmp[i]);
  delete [] tmp;
  return out;
}

}
