首先你的 int array[] = new int[2]; array[1]=20; array[2]=10; Arrays.sort(array); 应该是: int array[] = new int[2]; array[0]=20; array[1]=10; Arrays.sort(array); /////!!!! 另外如果要满足你的要求,那就自己来排序好了!
先定义一个: private Integer _indexes[] = null; private Object _needSort[] = null; private int _indexes; private boolean _ascending; 然后定义: public void reallocateIndexes() { _indexes = new Integer[_needSort.length]; // Initialise with the identity mapping. for (int r = 0; r < _indexes; r++) { _indexes[r] = new Integer(r); } }对它进行排序: public void sortByColumn(final boolean ascending) { try { _ascending = ascending; //we must reallocate here, //if we use old value, we may not sort correctly. reallocateIndexes(); java.util.List list = Arrays.asList(_indexes); Collections.sort(list, new Comparator(){ public int compare(Object r1,Object r2) { try { // get the real object that we want to sort. // i.e. we do not sort by r1 or r2. // but compair o1 and o2. int i1 = ((Integer)r1).intValue(); int i2 = ((Integer)r2).intValue(); Object o1 = _needSort[i1] Object o2 = _needSort[i2] // If both values are null, return 0. if (o1 == null && o2 == null) { return 0; } else if (o1 == null) { // Define null less than everything. return _ascending ? -1 : 1; } else if (o2 == null) { return _ascending ? 1 : -1; } // if can not compare o1 and o1, change the type of them if(!(o1 instanceof Comparable) || !(o2 instanceof Comparable)) { o1 = o1.toString(); o2 = o2.toString(); } int gt = ((Comparable)o1).compareTo(o2); return _ascending ? gt : (-1)*gt; } catch(Exception e) { ErrorLog.OutputLog(e); return 0; } } }); //After sort, we get this result. //That is result what we need _indexes = (Integer[])list.toArray(); } catch(Exception e) { ErrorLog.OutputLog(e); } }
int array[] = new int[2];
array[1]=20;
array[2]=10;
Arrays.sort(array);
应该是:
int array[] = new int[2];
array[0]=20;
array[1]=10;
Arrays.sort(array);
/////!!!!
另外如果要满足你的要求,那就自己来排序好了!
private Integer _indexes[] = null;
private Object _needSort[] = null;
private int _indexes;
private boolean _ascending;
然后定义:
public void reallocateIndexes()
{
_indexes = new Integer[_needSort.length];
// Initialise with the identity mapping.
for (int r = 0; r < _indexes; r++)
{
_indexes[r] = new Integer(r);
}
}对它进行排序:
public void sortByColumn(final boolean ascending)
{
try
{
_ascending = ascending;
//we must reallocate here,
//if we use old value, we may not sort correctly.
reallocateIndexes();
java.util.List list = Arrays.asList(_indexes);
Collections.sort(list, new Comparator(){ public int compare(Object r1,Object r2)
{
try
{
// get the real object that we want to sort.
// i.e. we do not sort by r1 or r2.
// but compair o1 and o2.
int i1 = ((Integer)r1).intValue();
int i2 = ((Integer)r2).intValue();
Object o1 = _needSort[i1]
Object o2 = _needSort[i2]
// If both values are null, return 0.
if (o1 == null && o2 == null)
{
return 0;
}
else if (o1 == null)
{
// Define null less than everything.
return _ascending ? -1 : 1;
}
else if (o2 == null)
{
return _ascending ? 1 : -1;
}
// if can not compare o1 and o1, change the type of them
if(!(o1 instanceof Comparable) || !(o2 instanceof Comparable))
{
o1 = o1.toString();
o2 = o2.toString();
}
int gt = ((Comparable)o1).compareTo(o2);
return _ascending ? gt : (-1)*gt;
}
catch(Exception e)
{
ErrorLog.OutputLog(e);
return 0;
}
}
});
//After sort, we get this result.
//That is result what we need
_indexes = (Integer[])list.toArray();
}
catch(Exception e)
{
ErrorLog.OutputLog(e);
}
}