sorting a collection

hello all,

i am tring to sort a two dim array of object , how can i print the dim array of object after sort?

package collection;

import java.util.Arrays;

import java.util.Collections;

import java.util.Comparator;

import java.util.List;

import java.util.ListIterator;

publicclass CollectionSort{

publicstaticvoid main(String args[])

{

Object[][] data =

{

{"Color","Red","Red"},

{"Shape","Square","Red"},

{"Fruit","Banana","Red"},

{"Plain","Text","Red"},

{"Plain","Text","Red"}

};

List list = Arrays.asList(data);

final Comparator stringCmp =new Comparator(){

String s1;

publicint compare(Object o1, Object o2){

s1 = o1.toString().toUpperCase();

String s2 = o2.toString().toUpperCase();

return s1.compareTo(s2);

}

public String toString(){

return s1;

}

};

Collections.sort(list, stringCmp);

for (ListIterator<Object> it = list.listIterator();

it.hasNext();){

String t = it.next().toString();

System.out.println(t);

}

}

}

[2816 byte] By [dayanandabva] at [2007-10-3 4:02:16]
# 1
what error are you getting? whats your question. I am not going to read all that code line by line. Please be more specific.
kilyasa at 2007-7-14 22:01:40 > top of Java-index,Core,Core APIs...
# 2
hello,thanks for u repaly, after sorting the list i am trying to print the sorted arraywith the help of ListIterator, but it printing hashcode something like that.regardsdaya
dayanandabva at 2007-7-14 22:01:40 > top of Java-index,Core,Core APIs...
# 3

Wonder, what you expect. You have an array of arrays, whereof you make a list of arrays. Your comparator as well as your output loop operate on array objects, on which you call toString(). For array objects, toString() of Object.class will be used which prints the class name plus hexadecimal hashcode. Neither your sort nor your output does, what you want.

stefan.schulza at 2007-7-14 22:01:40 > top of Java-index,Core,Core APIs...
# 4
hello,i am trying to sort two dim array and print the sorted array.can u help to sort two dim, or give me a suggestionthanksdaya
dayanandabva at 2007-7-14 22:01:40 > top of Java-index,Core,Core APIs...
# 5
1. http://forum.java.sun.com/thread.jspa?threadID=676578&messageID=39497512. Don't use Arrays, unless you cannot solve it using OO (i.e., comparable objects in a list) or OO would be to expensive.
stefan.schulza at 2007-7-14 22:01:40 > top of Java-index,Core,Core APIs...
# 6

You mustn't create something already created:

Collections.sort(list, String.CASE_INSENSITIVE_ORDER);

See: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#CASE_INSENSITIVE_ORDER

Respect toString(), I have created a recursive toString for any object, included arrays or matrix:

public static String toString(Object object){

String string;

if(object.getClass().isArray()){

StringBuilder arrayString=new StringBuilder("[");

int length=Array.getLength(object);

for(int i=0;i<length;i++){

arrayString.append(toString(Array.get(object,i)));

arrayString.append(", ");

}

arrayString.setCharAt(arrayString.length()-2,']');

string=arrayString.substring(0,arrayString.length()-1);

}

else

string=String.valueOf(object);

return string;

}

>

govisagod512a at 2007-7-14 22:01:40 > top of Java-index,Core,Core APIs...