Faqts : Business : Programming : Shopping For You : Java

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

1 of 3 people (33%) answered Yes
Recently 1 of 3 people (33%) answered Yes

Entry

I have an array of objects consisting of ints and strings, how can i sort it using the ints?

Oct 27th, 2004 22:30
Tom McGuire, Gareth Bowman,


The real question is: During the sort, what would you like to happen to 
the integers?

Java contains a good implementation of mergesort which, for Objects 
which implement the java.lang.Comparable interface can be used quite 
simply:
   
   Arrays.sort(myArray);

In this case (assuming that you want the Strings to remain in the array 
and just push them off to one side or the other) you can write your 
own "external comparison" logic into an Object which implements the 
java.util.Comparator interface. The code would look something like this:

   Arrays.sort(myMixedArray, new Comparator() {
      public boolean equals(Object obj) {
	return false;
      }

      public int compare(Object o1, Object o2) {
         if(o1 instanceof Integer){
            if(o2 instanceof Integer){
               //case 0: 
               return ((Integer)o1).compareTo((Integer)o2);
            } else {
               //case 1:
	       return -1;
            }
         } else {
            if(o2 instanceof Integer){
              //case 2:
	      return 1;
  	    }
         }
         //case 3:
         return 0;
      }
   });


In the above example the following cases are commented:
case 0: The two array elements are both of type Integer and can be 
compared using the "compareTo" method defined by Integer. If you wanted 
to change the logic (i.e. have bigger ints appear before smaller ints, 
then that logic change should be implemented here).

case 1: One of the two elements is an int, and the other is not. Since 
a comparison cannot be made, return -1 in this case to push the int up 
in the list.

case 2: As in case 1, one of the elements is an Integer; we return 1 
here to push the non-Integer down in the list.

case 3: Neither of the elements is an Integer. If you care how the 
Strings are sorted, then you would return the result of String.compareTo
(). Otherwise, return a 0 indicating that the two Strings are 
effectively equivalent for the purposes of the sort.

Notes: Swapping the return values in cases 1 and 2 will result in an 
array with the Strings in the first part of the array and the Integers 
pushed to the end.

Also--one interpretation could be that you want to parse the Strings 
and use the integer result for comparison with other "true" Integers in 
the array. In this case, you would do replace case 1 and 2 with:

//case 1:
return ((Integer)o1).compareTo(new Integer((String)o2));

//case 2:
return (new Integer((String)o1).compareTo((Integer)o2));

The above code is bare-bones. To make it bullet-proof additional 
testing for the String type is highly recommended!!!!