You can try the following..
This is the Object for each row data.
------------------------------------
import java.util.*;

public class TwoDArraySort implements Comparable
{
        // This Map will contain something like
        // col1=1, col2=123,col3=23..
        private Map _colNameWithVal;
        
        TwoDArraySort(Map val)
        {
                _colNameWithVal = val;
        }
        
        public Map getAllData()
        {
                return _colNameWithVal;
        }
        
        public Object getVal(String colName)
        {
                return _colNameWithVal.get(colName);
        }
        
        public int compareTo(Object o1)
        {
                return 0;
        }
        
}
--------------------------------------------
This is the gerenic comparator sorter. Here u can add other cases like
Date..
import java.util.*;

public class TwoDArrayComparator implements Comparator
{
    String sorter;

    public TwoDArrayComparator(String sorter) {
        this.sorter = sorter;
    }

    public int compare(Object obj1,Object obj2)
    {
        System.out.println();
        TwoDArraySort tdas1 = (TwoDArraySort)obj1;
        TwoDArraySort tdas2 = (TwoDArraySort)obj2;
        int diff = 0;
                
        if (sorter == null )
      {
         int tmp = tdas1.compareTo( tdas2 );
         if ( tmp != 0)  return tmp;
         else return 1;
      }
        else
        {
          System.out.println("this.sorter="+this.sorter+" tdas1="+tdas1);
          Object val1 = tdas1.getVal(this.sorter);
          Object val2 = tdas2.getVal(this.sorter);
                        
          if  (val1 instanceof Integer)
        {
           int value1 = ((Integer)val1).intValue() ;
           int value2 = ((Integer)val2).intValue();
           diff = (value2 - value1);
                System.out.println("Case Integer: value2="+value2+"
value1="+value1);
        }
        else if (val1 instanceof Long)
        {
          long value1 = ((Long)val1).longValue();
          long value2 = ((Long)val2).longValue();
          diff = (value2>value1)?1:((value2==value1)?0:-1);
            System.out.println("Case Long: value2="+value2+"
value1="+value1);
        }
        else if (val1 instanceof Double)
        {
          double value1 = 100*((Double)val1).doubleValue() ;
          double value2 = 100*((Double)val2).doubleValue();
          diff = (value2>value1)?1:((value2==value1)?0:-1);
            System.out.println("Case Double: value2="+value2+"
value1="+value1);
        }
        else
        {
          String strVal1 = val1.toString();
          String strVal2 = val2.toString();

          diff = strVal2.compareTo(strVal1);
          System.out.println("Case Else: strVal2="+strVal2+"
strVal1="+strVal1);
       }
       if (diff != 0)
         return diff;
       else
         return tdas1.compareTo( tdas2 );
     }                  
    }
    
}

-------------------------------------------------------------------
This is the sample Driver program
import java.util.*;
public class Driver
{
  public static void main(String[] args )
  {
        TwoDArraySort[] twoDs = new TwoDArraySort[3];
                
                Map colWithVal = new HashMap(3);
                colWithVal.put("col1",Integer.valueOf("1"));
                colWithVal.put("col2",Integer.valueOf("123"));
                colWithVal.put("col3",Integer.valueOf("23"));

                twoDs[0] = new TwoDArraySort(colWithVal);
                
                colWithVal = new HashMap(3);
                colWithVal.put("col1",Integer.valueOf("3"));
                colWithVal.put("col2",Integer.valueOf("34"));
                colWithVal.put("col3",Integer.valueOf("34"));
                twoDs[1] = new TwoDArraySort(colWithVal);
                
                colWithVal = new HashMap(3);
                colWithVal.put("col1",Integer.valueOf("23"));
                colWithVal.put("col2",Integer.valueOf("324"));
                colWithVal.put("col3",Integer.valueOf("314"));
                twoDs[2] = new TwoDArraySort(colWithVal);
                
                System.out.println("Before sorting..");
                for (int i=0; i<3; i++)
                {
                        System.out.println(i+">>"+twoDs[i].getAllData());
                }
                Arrays.sort(twoDs, new TwoDArrayComparator("col2"));
                System.out.println("After sorting..");
                for (int i=0; i<3; i++)
                {
                        System.out.println(i+">>"+twoDs[i].getAllData());
                }
  }
}

Hope it will help you.

Nikhil R Silsarma
Developer
DigitalRUM UK Ltd.
Tel: 020 7604 2049



-----Original Message-----
From: KEVIN LI [mailto:[EMAIL PROTECTED]]
Sent: 26 February 2002 19:36
To: JRun-Talk
Subject: 2d array sorting


Hi .
did any one knows there is some better way to do 2D array sort.
suppose I have some data like
[ col1  col2            col3            col4]
[1              123             23              234]
[3              34              34              34]
[23             343             34              34]
and I want sort them by any  column .


Thanks

Kevin

______________________________________________________________________
Your ad could be here. Monies from ads go to support these lists and provide more 
resources for the community. http://www.fusionauthority.com/ads.cfm
Archives: http://www.mail-archive.com/[email protected]/
Unsubscribe: http://www.houseoffusion.com/index.cfm?sidebar=lists

Reply via email to