aspose file tools*
The moose likes Java in General and the fly likes Comparator and Sorting Arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Comparator and Sorting Arrays" Watch "Comparator and Sorting Arrays" New topic
Author

Comparator and Sorting Arrays

Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 819
Here's the code segment I have :
//***** For comparison table ****
java.util.Set setObj2 = new java.util.TreeSet(new java.util.Comparator() {
public int compare(Object o1, Object o2) {
return compare((Object[]) o1, (Object[]) o2);
}
public int compare(Object[] o1, Object[] o2) {
return (((Double) o2[1]).compareTo((Double) o1[1]));
}
});
for (int i = 1; i <= currItem; i++) {
setObj2.add(new Object[] { elems3[i], new Double(Double.parseDouble(elems4[i]))});
}

After this if I do a setObj2.size() I get the count 1 less than what I was expecting. The arrays passed in have 3 values each whereby the second and third values for elems2 are the same (i.e. 0). How can I change the code to allow for the same values to be accepted and tested accordingly? The data is similar to this:
elems[0] -> 01 January elems2[0] -> 65990.00
elems[1] -> 02 February elems2[1] -> 0
elems[2] -> 03 March elems2[2] -> 0

Thanks.

Rob
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Hi Rob,

First of all, I just wanted to point out that Java array elements are numbered from 0 upwards, and loops generally look like

for (int i=0; i<limit; ++i)

If you're deliberately doing something different, then that's fine, but I just wanted to make sure you knew that. I'm hoping this is not code that allocates an extra element at the end and then uses indexes starting from 1 -- that's very unnerving to veteran Java programmers.

Furthermore, it looks to me -- although I could be wrong -- as if you're using Object[]s as "poor man's objects." This makes the resulting code hard to understand, with lots of extra casts and things that don't make any sense without comments. Are you sure the small Object[]s you're allocating here shouldn't be instances of a little class created for this purpose? The name of the class, and the names of its members, would help to document your intent.

Now, as far as your question: as I understand it, you're seeing a TreeSet that's smaller than you expect because there are duplicate items. Note that the definition of "duplicate" here is that the Comparator you write returns 0, which it will when two Doubles are equal. Again, I think you understand that.

Now, if you don't want the duplicate-removal behavior, then you really shouldn't be using a Set -- you could use a List instead, for example.

Alternatively, you can make your Comparator smarter: for example, if the Double.compareTo() method returns 0, it could then compare some other objects in your arrays, and return the result of that comparison. That could serve as a "tiebreaker." Of course, then you have to worry about that comparison returning 0, too!


[Jess in Action][AskingGoodQuestions]
Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 819
Hi Eric,
Yeah the first element coming from the data I've been given is a column header (more or less), everything after is the data for that column (hence starting at 1). Most of the example was shown to me by another guy I know on the web so when he referred to Objects I went with Objects (don't get me wrong I'm extremely grateful to him for the help as otherwise I'd be stuck with something far more messy than what he would post). I tried putting in if the 2 were equal then just return 1 to see what would happen and it still gave me 2 entries as opposed to the 3 I was expecting. You mentioned List, can I simply replace any references to Set to get this working? Will the List behave any differently when it comes to sorting then if the same Comparator is used? The data is generated elsewhere using a Perl script but the end results will be in a JSP (this is where I have this code), just to give a little background to what's being done. Thanks for the response, right now I appreciate any help given even more.

Rob
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Instead of a TreeSet, you could use an ArrayList, and yes, it would be used almost identically, except that instead of supplying the Comparator in the constructor, you would use the Comparator to explicitly sort the list after populating it:

java.util.Collections.sort(theArrayList, new Comparator() { ...
Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 819
Thanks Eric. I'll give that a try. Right now I'm going to add an additional unique identifier to the TreeSet I'm creating. In the Comparator I'll then check the values I'm checking and, if they are the same do a comparison of the unique values. When I get a chance (and if I remember to) I'll post what I get from the ArrayList. Thanks again for the response.

Rob
Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 819
Quick question: are Arrays used much anymore? I've heard someone say that ArrayList are used more because they are better and they are re-sizable (similar I guess to redim-ing in VB). Is that true?

Rob
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Originally posted by Rob Hunter:
Quick question: are Arrays used much anymore? I've heard someone say that ArrayList are used more because they are better and they are re-sizable (similar I guess to redim-ing in VB). Is that true?


ArrayList is implemented as an array plus the code needed to redimension it -- i.e., reallocate and copy. Using raw arrays is fine when they're appropriate, and in fact Java 5 introduced some new machinery to let you treat arrays and collections like ArrayList uniformly in some circumstances.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Comparator and Sorting Arrays