Index: trunk/MagicSoft/Mars/Changelog
===================================================================
--- trunk/MagicSoft/Mars/Changelog	(revision 1509)
+++ trunk/MagicSoft/Mars/Changelog	(revision 1511)
@@ -1,3 +1,14 @@
                                                                   -*-*- END -*-*-
+
+ 2002/09/02: Thomas Bretz
+
+   * mhist/MHMatrix.[h,cc]:
+     - added GetIndexdOfSortedColumn
+     - added SortMatrixByColumn
+
+   * mhist/HistIncl.h:
+     - added TArrayI.h (for MHMatrix.h)
+
+
 
  2002/08/19: Thomas Bretz
@@ -28,4 +39,5 @@
      - added comments
      - added another sanity check in SetBinning(TH1*,TH1*)
+
 
 
Index: trunk/MagicSoft/Mars/mhist/HistIncl.h
===================================================================
--- trunk/MagicSoft/Mars/mhist/HistIncl.h	(revision 1509)
+++ trunk/MagicSoft/Mars/mhist/HistIncl.h	(revision 1511)
@@ -2,4 +2,5 @@
 
 #include <TF1.h>
+#include <TArrayI.h>
 
 #endif // __CINT__
Index: trunk/MagicSoft/Mars/mhist/MHMatrix.cc
===================================================================
--- trunk/MagicSoft/Mars/mhist/MHMatrix.cc	(revision 1509)
+++ trunk/MagicSoft/Mars/mhist/MHMatrix.cc	(revision 1511)
@@ -49,4 +49,5 @@
 
 #include <TList.h>
+#include <TArrayF.h>
 #include <TArrayD.h>
 #include <TArrayI.h>
@@ -482,2 +483,38 @@
             out << "   " << GetUniqueName() << ".AddColumn(\"" << (*fData)[i].GetRule() << "\");" << endl;
 }
+
+const TArrayI MHMatrix::GetIndexOfSortedColumn(Int_t ncol, Bool_t desc) const
+{
+    TMatrixColumn col(fM, ncol);
+
+    const Int_t n = fM.GetNrows();
+
+    TArrayF array(n);
+
+    for (int i=0; i<n; i++)
+        array[i] = col(i);
+
+    TArrayI idx(n);
+    TMath::Sort(n, array.GetArray(), idx.GetArray(), desc);
+
+    return idx;
+}
+
+void MHMatrix::SortMatrixByColumn(Int_t ncol, Bool_t desc)
+{
+    TArrayI idx = GetIndexOfSortedColumn(ncol, desc);
+
+    const Int_t n = fM.GetNrows();
+
+    TMatrix m(n, fM.GetNcols());
+    for (int i=0; i<n; i++)
+    {
+        TVector vold(n);
+        vold = TMatrixRow(fM, idx[i]);
+
+        TMatrixRow rownew(m, i);
+        rownew = vold;
+    }
+
+    fM = m;
+}
Index: trunk/MagicSoft/Mars/mhist/MHMatrix.h
===================================================================
--- trunk/MagicSoft/Mars/mhist/MHMatrix.h	(revision 1509)
+++ trunk/MagicSoft/Mars/mhist/MHMatrix.h	(revision 1511)
@@ -9,4 +9,5 @@
 #endif
 
+class TArrayI;
 class MDataArray;
 
@@ -59,4 +60,7 @@
     void Reassign();
 
+    const TArrayI GetIndexOfSortedColumn(Int_t ncol=0, Bool_t desc=kTRUE) const;
+    void SortMatrixByColumn(Int_t ncol=0, Bool_t desc=kTRUE);
+
     ClassDef(MHMatrix, 1) // Multidimensional Matrix to store events
 };
